Using OnClick in Forms to create multiple OnSubmit options

Notice: This thread is very old.
retalskram
Member | 4
+
0
-

I am trying to modify the Nette tutorial edit form, to create multiple onSubmit buttons – a Publish – and a Cancel. But I'm not getting very far with any of the documents explaining the use of this.

In my PostPresenter I have

protected function createComponentPostForm() {
        $form = new Form;
        $form->addText('headline', 'Headline:')
                ->setRequired();
        $form->addTextArea('quote', 'Quote:')
                ->setRequired();

		$form->addSubmit('send', 'Save and publish')
                ->onClick[]=[$this, 'postFormSucceeded'];
		$form->addSubmit('cancel', 'Cancel')
                ->onClick[]=[$this, 'postFormCancelled'];
        return $form;
    }

But although it calls the function postFormSucceeded, it doesn't seem to send the necessary arguments ($form, $values). So I get this error
Missing argument 2 for App\Presenters\PostPresenter::postFormSucceeded()

David Matějka
Moderator | 6351
+
+2
-

Hi, arguments passed to onSuccess, onSubmit and onClick event callbacks are different.

  • for onSuccess you will get $form and $values
  • for onSubmit just a $form
  • and for onClick only $button. but you can get form via $button->getForm() and values via $button->getForm()->getValues()
retalskram
Member | 4
+
0
-

David Matějka wrote:

Hi, arguments passed to onSuccess, onSubmit and onClick event callbacks are different.

  • for onSuccess you will get $form and $values
  • for onSubmit just a $form
  • and for onClick only $button. but you can get form via $button->getForm() and values via $button->getForm()->getValues()

Thanks David.

So then I have to add a line in my target function? like $form = $button->getForm();

retalskram
Member | 4
+
0
-

I have changed the argument of my function to postFormSucceeded($button)

And then I am able to declare variable $values = $button->getForm()->getValues()

Is that the right way to do it?

CZechBoY
Member | 3598
+
+4
-

Or you can call it from anonymous function

$form->addSubmit('send')
->onClick[] = function ($button) {
    $form = $button->getForm();
    $values = $form->getValues();
    $this->postFormSucceeded($form, $values);
};