Home > database >  Symfony validation form of two identical fields
Symfony validation form of two identical fields

Time:09-30

I use standalone Symfony components in my app (and without Twig).

My HTML form contains two fields ('new_password' & 'confirm_new_password').

The following validation works fine:

$validator = Validation::createValidator();
$violations = $validator->validate($new_password, [
    new Length(['min' => 4]),
    new Regex([
        'pattern' => '/\d/',
        'match' => true,
        'message' => 'Password must contain at least one number'
    ])
]);

if (0 !== count($violations)) { 
    ...
}

I would like to add validation of password confirmation fields as well

The 'Form' component by Symfony allows to create, process and reuse forms, but this is far beyond of what I want to do. I found that 'RepeatedType' field by Symfony can do this but seems to be using the 'Form' component.

How can I simply add password confirmation to my validation script?

CodePudding user response:

there is a constraint call identicalTo that looks quite the same as explained by RiggsFolly. So you call this constraint and give it both fields' values.

  • Related