Symfony2: $ form-> isValid () always returns true

I have a problem with Symfony form validation (using Silex). I have an input repeated

for a password reset which works fine if both passwords are the same. However, if they don't, it $form->isValid()

returns true. $form->getErrorsAsString()

is also empty. After a lot of searching and reading many answers to questions that were more or less related, I still haven't found a solution.

Update: After checking various versions from my Git repository, I eventually found that this feature broke when I upgraded 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 switched back temporarily but would end up using the version 2.6.0

...

Here's the method buildForm

defined for mine 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 template associated with 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) }}

      

Related 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

        /* ... */
    }
});

      

It generates the following error if the passwords do not match: Error: ContextErrorException in controllers_users.php line 113: Notice: Undefined index: password

. This means $form->isValid()

returned true

until it should have, since the passwords don't match.

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

+3


source to share


1 answer


This turned out to be a bug in Symfony and was fixed in 2.6.6. See my bug report on GitHub for more information .



0


source







All Articles