Name of the submit button
- mrataja
- Member | 57
Hi all, how can I get submit button name?
Just a short example:
<?php
class Page
{
protected function createComponentPagesEditForm()
{
$form = new UI\Form();
$form->addText('title', 'Titulek');
$form->addSubmit('send', 'Uložit');
$form->addSubmit('sendAndClose', 'Uložit a zavřít');
$form->onValidate[] = callback($this, 'validatePagesEditForm');
return $form;
}
public function validatePagesEditForm($form)
{
$values = $form->getValues();
\Nette\Diagnostics\Debugger::dump($form->isSubmitted()); //true
\Nette\Diagnostics\Debugger::dump($form['send']->isSubmittedBy()); //false
\Nette\Diagnostics\Debugger::dump($form['sendAndClose']->isSubmittedBy()); //false
}
}
?>
I am trying it according to specification https://doc.nette.org/en/forms#… but there si something I cannot see.
Thank you for help
- petr.pavel
- Member | 535
This is weird. $form->isSubmitted() is supposed to return control/button that triggered the submission. It only returns true when it's not submitted by any submit button.
If you look into $control->isSubmittedBy(), you'll see it calls $form->isSubmitted() and compares the result against itself.
My guess is you didn't submit the form by clicking either of the submit buttons.
Or the form is disabled, or it's not attached to a presenter. I see createComponentPagesEditForm() doesn't pass parent to UI\Form constructor. How do you attach the form to the presenter/component?
- Tomáš Votruba
- Moderator | 1114
I just copy pasted and tried your example and it works as expected.
Nette 2.3-dev. Your version?
- mrataja
- Member | 57
As in most cases, this was my mistake
I am rewriting old system and as you can see below, it was poorly defined
<?php
<input n:name=send type="submit" class="button green save" value="Uložit" name="odeslat">
<input type="submit" class="button green" value="Uložit a zavřít" name="odeslat_a_zavrit">
\Nette\Diagnostics\Debugger::dump($form['send']->isSubmittedBy()); //false
\Nette\Diagnostics\Debugger::dump($form['sendAndClose']->isSubmittedBy()); //false
?>
But after your posts, i have double checked it once again and now it working like charm.
<?php
<input n:name=send type="submit" class="button green save" value="Uložit">
<input n:name=sendAndClose type="submit" class="button green" value="Uložit a zavřít">
\Nette\Diagnostics\Debugger::dump($form['send']->isSubmittedBy()); //true - depends on clicked submit
\Nette\Diagnostics\Debugger::dump($form['sendAndClose']->isSubmittedBy()); //true - depends on clicked submit
?>
Thank you boys
- Tomáš Votruba
- Moderator | 1114
Just few tips to prevent errors in future:
1. set class in $form
definition, rather than via manual
rendering
$form->addSubmit('send', 'Uložit')
->setAttribute('class', 'button green save');
2. use dump
dump(...);
is alias to
\Nette\Diagnostics\Debugger::dump(...);