Symfony2表单isclicked不起作用

时间:2014-04-25 12:49:55

标签: symfony

我试图提交一个带有2个按钮的表单,但该方法被点击不起作用...

$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;

在我的树枝下面:

<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>

但是按下我按下的按钮这个代码总是显示&#34;重置&#34; ...

并不重要

有什么问题? :/

提前谢谢!

1 个答案:

答案 0 :(得分:1)

首先我注意到你没有handleRequest

$request = $this->get('request');
if($request->getMethod() == 'POST'){
    $form->handleRequest($request); // <== THIS

    $value = $form->get('save')->isClicked()  ?'save' :'reset';
    echo $value;

希望这会有所帮助......