Reputation: 788
I'm trying to submit a form with 2 buttons but the method isclicked doesn't work...
$user = $repository->find($id);
$formBuilder = $this->createFormBuilder($user);
$form = $formBuilder->add('username', 'text',array(
'label' => "Pseudo :"))
->add('email', 'text', array(
'label' => "E-mail address :"))
->add('save', 'submit',array(
'label' => 'Save',
'attr' => array( 'class' => 'btn btn-primary')))
->add('reset', 'submit',array(
'label' => 'Reset pwd',
'attr' => array( 'class' => 'btn btn-primary')))
->getForm();
$request = $this->get('request');
if($request->getMethod() == 'POST'){
$value = $form->get('save')->isClicked() ?'save' :'reset';
echo $value;
Below my twig:
<section>
{{ form_start(form) }}
{{form_errors(form) }}
<div class='form_group'>
{{form_row(form.username)}}
{{form_row(form.email)}}
<br>
<div class='testbutton'>
{{form_row(form.save)}}
{{form_row(form.reset)}}
</div>
{{ form_end(form) }}
</div>
</section>
But it doesn't matter which button I pressed this code will always display "reset"...
What is wrong? :/
Thank you in advance !
Upvotes: 0
Views: 1633
Reputation: 20191
First thing I noticed that you didn't handleRequest
:
$request = $this->get('request');
if($request->getMethod() == 'POST'){
$form->handleRequest($request); // <== THIS
$value = $form->get('save')->isClicked() ?'save' :'reset';
echo $value;
Hope this helps...
Upvotes: 1