dongmi1864 2014-11-30 18:55
浏览 193
已采纳

Symfony2:$ form-> isValid()总是返回true

I'm having a problem with Symfony form validation (using Silex). I have a repeated input for a password reset form, which works fine if both passwords match. If they don't, however, $form->isValid() still returns true. $form->getErrorsAsString() is empty too. After lots of googling and reading many answers to questions here that were more or less related, I still haven't found a solution.

Update: After checking out various versions from my Git repository, I eventually found out the feature broke when I updated Symfony from version 2.5.7 to version 2.6.0 released yesterday. I looked at the changelog and didn't see anything that would break this. I temporarily switched back, but would eventually like to be able to use version 2.6.0...

Here's the buildForm method defined for my PasswordResetType:

function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('password', 'repeated', array(
                'type' => 'password',
                'invalid_message' => $this->app['translator']->trans('form_errors.pass_must_match'),
                'required' => true,
                'first_options' => array(
                    'constraints' => array(
                        new Assert\NotBlank()
                    ),
                ),
                'second_options' => array(
                    'constraints' => array(
                        new Assert\NotBlank()
                    )
                )
            ))
        ;
    }
}

The Twig template associated to my form:

{{ form_start(form) }}
    {{ form_widget(form._token) }}
    {{ form_errors(form) }}

    <div class="form-group">
        {{ form_label(form.password.first, 'user.new_password'|trans, {'label_attr': {'class': 'control-label'}}) }}
        {{ form_errors(form.password.first) }}
        {{ form_widget(form.password.first, {'attr': {'class': 'form-control', 'placeholder' : 'user.new_password'|trans}}) }}
    </div>

    <div class="form-group">
        {{ form_label(form.password.second, 'user.new_password_confirmation'|trans, {'label_attr': {'class': 'control-label'}}) }}
        {{ form_errors(form.password.second) }}
        {{ form_widget(form.password.second, {'attr': {'class': 'form-control', 'placeholder' : 'user.new_password_confirmation'|trans}}) }}
    </div>
    <button type="submit" class="btn btn-primary btn-block">{{'user.reset_password'|trans}}</button>
{{ form_end(form) }}

The associated controllers:

$app->get('/{_locale}/reset_password/{token}', function(Request $request, $token) use ($app) {
    /* ... */

    $form = $app['form.factory']->createBuilder(new PasswordResetType($app), array())->getForm();

    return $app['twig']->render('password_reset.twig', array(
        'title' => 'Page title',
        'form' => $form->createView()
    ));
})->bind('reset_password');

$app->post('/{_locale}/reset_password/{token}', function(Request $request, $token) use ($app) {
    /* ... */

    $form = $app['form.factory']->createBuilder(new PasswordResetType($app), array())->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $data = $form->getData();

        $password = $data['password']; // this is line 113

        /* ... */
    }
});

This generates the following error when passwords don't match: Error: ContextErrorException in controllers_users.php line 113: Notice: Undefined index: password. This indicates $form->isValid() returned true while it shouldn't have, since the passwords don't match.

Update: I opened a bug report: https://github.com/symfony/symfony/issues/12792

  • 写回答

1条回答 默认 最新

  • dpdjv9559 2016-08-25 07:57
    关注

    This ended up being a bug in Symfony, which was fixed in version 2.6.6. See my bug report on GitHub for more information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?