Laminas\Form extension not resolving in IndexController

I added a Laminas\Form extension called AddjobForm in the ‘module\Application\src\Form’ folder with namespace Application\Form

When I try to create a new AddjobForm() in the IndexController of Application module I get the error
Class Application\Form\AddjobForm not found.

I have added the use namespace to Index controller as well. please help.

Hello :slight_smile:

Do you have a ServiceConfig for Application\Form\AddjobForm in Module.php?

public function getServiceConfig(): array
{
    return [
        \Application\Form\AddjobForm::class => function( $sm ) {
             return new \Application\Form\AddjobForm(
                #You can use $sm->get(\Other\Class::class) for DI in your constructor.
            );
        }
    ];
}

I hope that it is what you mean. I am a Newbie in Laminas but it is a great framework

In the documentation of laminas-form you can found the following description: Application Integration – Usage in a laminas-mvc Application

Hello and welcome to the forums! :smiley:

Please provide the filename and the full code of your form class, this allows us to check for problems. Thanks!

Not needed here and also should avoided. If no separate factory for the form is needed then you need no extra registration. See the description for application integration from above for more details.

I decided to use the Application module itself instead of creating a separate module. This was done just for study purposes. And I feel the issue I am facing has something to do with that.
I have not registered a factory for this since I am directly using the new operator to create the AddjobForm instance. Please refer to the screenshot of the error message below. It appears to be dumping code from the AddjobForm class.

Error

File:
C:\Users\Siddhartha\OneDrive\code\laminasmassmailer\module\Application\src\Controller\IndexController.php:39

Message:
Class ‘Application\Form\AddjobForm’ not found

Code for Application\src\Form\AddjobForm.php

namespace Application\Form;
use Laminas\Form\Form;

class AddjobForm extends Form
{

	public function __construct($name = null)
	{

		parent::__construct('jobsummary');

		$this->add([
			'name' => 'jobid',
			'type' => 'hidden',
		]);
              ... 

Code for Application\src\Controller\IndexController.php

I was expecting that IndexController would be able to resolve the AddjobForm class especially since it is explicitly declared with the use keyword.

namespace Application\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Laminas\Form\Factory;
use Application\Model\JobsummaryTable;
use Application\Model\Jobsummary;
use Application\Form\AddjobForm;

class IndexController extends AbstractActionController
{

    private $jobsummaryTable;

    public function __construct(JobsummaryTable $jobsummarytable)
    {
        $this->jobsummaryTable = $jobsummarytable;
    }

    public function indexAction()
    {
        return new ViewModel([
            'jobsummaries' => $this->jobsummaryTable->fetchAll(),
        ]);
    }

    public function addjobAction()
    {
        $form = new AddjobForm();  -- this is where i get the error. this is line no. 39
        $request = $this->getRequest();
        if(!$request->isPost())
        {
            //blank form
            return ['form' => $form];
        } 
        ...

Screenshot of error message

I figured this out. There was a syntactical error in the phtml which was strangely causing this error.

Great! :+1:

Which error? I would like to reproduce the problem.

Is ‘type’ => hidden ok???

$this->add(
    [
        'name'       => 'jobid',
        'type'       => Laminas\Form\Element\Text::class,
        'attributes' => [
            'hidden' => true,
        ],
    ]
);

Sure, it is a separate element: Laminas\Form\Element\Hidden

Ah, ok, I found Hidden Element just. :smiley:
But is there an alias ‘hidden’ for Hidden::class?

Correct. The FormElementManager is a plugin manager and contains all references:

The Form class uses the FormElementManager via the factory when you add an element with the specification in an array.

Perfect. I learn so much here. Thanks. :slight_smile: