Symfony2:$ form-> isValid()始终返回true

时间:2014-11-30 18:55:39

标签: php forms validation symfony silex

我遇到Symfony表单验证问题(使用Silex)。我有一个repeated输入密码重置表单,如果两个密码匹配,它可以正常工作。但是,如果他们没有,$form->isValid()仍然返回true。 $form->getErrorsAsString()也是空的。 经过大量谷歌搜索并阅读了许多或多或少相关的问题答案,我仍然没有找到解决方案。

更新:从我的Git存储库中检出各种版本后,我最终发现当我将Symfony从版本2.5.7更新为昨天发布的版本2.6.0时,该功能已损坏。我查看了更改日志,并没有看到任何会破坏这一点的内容。我暂时转回来,但最终希望能够使用版本2.6.0 ...

以下是为buildForm定义的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()
                    )
                )
            ))
        ;
    }
}

与我的表单关联的Twig模板:

{{ 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) }}

关联的控制器:

$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

        /* ... */
    }
});

如果密码不匹配,则会生成以下错误:Error: ContextErrorException in controllers_users.php line 113: Notice: Undefined index: password。这表示$form->isValid()已退回true,但由于密码不匹配,因此不应该{。}}。

更新:我打开了一个错误报告:https://github.com/symfony/symfony/issues/12792

1 个答案:

答案 0 :(得分:0)

这最终成为Symfony中的一个错误,已在2.6.6版中修复。有关详细信息,请参阅GitHub上的bug report

相关问题