Passing params to form component – value of params is null

9 months ago

SamuelThorn
Member | 18
+
0
-

Hi,

I have just solved a mystery that took me a lot of time. I am posting this as I think it could help someone.

I had this form as a component, and I wished to pass some params to it.

The component:

class EditForm extends UI\Control
{
    public $params;

    public function __construct($params)
    {
        parent::__construct();
        $this->params = $params;
        bdump($this->params); // (1) I will  write about this later
    }

    public function createComponentForm()
    {
        bdump($this->params); // (2) I will  write about this later

        $form = new UI\Form;

        /* form fields */

        $form->onSuccess[] = [$this, 'processForm'];

        return $form;
    }

    public function render()
    {
        $this->template->setFile(__DIR__.'/templates/editProgram.latte');
        $this->template->render();
    }

    /**
     * @param UI\Form $form
     */
    public function processForm(UI\Form $form)
    {
        /* some data processing code */
        $form->onSuccess;
    }
}

Its factory:

interface IEditProgramFormFactory
{
    /**
     * @return EditProgramForm
     */
    function create($params);
}

Which i call like this:

$form = $this->editProgramFormFactory->create([/* some values*/])

At first look nothing out of ordinary, but when I tried to run it, the first bdump (1) returned the params as expected, but the second one (2) was returning null no matter what I have tried to do.

Finally i tried to rename the $params to something else (in my case $pamareters) and voilà it all has started to work normally.

Is $params some sort of “reserved variable”, or was doing something wrong?

Last edited by SamuelThorn (2019-03-02 15:35)

9 months ago

nightfish
Member | 153
+
0
-

SamuelThorn wrote:
Is $params some sort of “reserved variable”, or was doing something wrong?

Yes, it is. See https://api.nette.org/…ent.php.html#147 for more details.

9 months ago

SamuelThorn
Member | 18
+
0
-

nightfish wrote:
Yes, it is. See https://api.nette.org/…ent.php.html#147 for more details.

I see now, thanks. But still… it was a nasty trap to crawl out from. ;-)