form handle method registration

4 years ago

kapcus
Member | 4
+
0
-

Hi,

with respect of future Nette and PHP development, what is the preferred way how to register handle method on form ?

<?php
$this->addSubmit('b_save', 'Save')->onSuccess[] = array($this, 'testFormSucceeded')
?>

or

<?php
$this->addSubmit('b_save', 'Save')->onSuccess[] = $this->testFormSucceeded
?>

Thx

4 years ago

Pavel Kravčík
Member | 995
+
+3
-

I prefer this one:

[$this, 'testFormSucceeded']

4 years ago

Henix
Member | 1
+
0
-

Im using callable method

$this->testFormSucceeded

4 years ago

CZechBoY
Member | 3407
+
+2
-

Or via anonymous function, which calls private handle method.

public function create ()
{
    $form->onSuccess[] = function (Form $form, ArrayHash $values) {
        $this->processForm($form, $values);
    };
}

private function processForm (Form $form, ArrayHash $values)
{
    // process
}

4 years ago

Pavel Kravčík
Member | 995
+
0
-

@CZechBoY: This shouldn't be preferred way. :)

4 years ago

David Matějka
Moderator | 5972
+
0
-

@CZechBoY I also use this very often. Great advantage is that static analysis (references, refactoring…) works well