I’m using Silex for a small project, but I’m not sure how to validate two matching password fields, also check for the uniqueness of an email using a database connection. I haven’t been able to figure it out in SF2 docs.
Possible someone can give me a hint or sample?
Thanks in advance
if ('POST' === $user->getMethod()) {
$constraint = new Assert\Collection(array(
'name' => array(new Assert\NotBlank(array('message' => 'Name shouldnt be blank'))),
'username' => array(new Assert\NotBlank(), new Assert\MinLength(3)),
'email' => array(new Assert\NotBlank(), new Assert\Email()),
'password' => array(new Assert\NotBlank(), new Assert\MinLength(6)),
'password2' => array(new Assert\NotBlank(), new Assert\MinLength(6)),
'terms' => array(new Assert\True()),
));
$errors = $app['validator']->validateValue($user->request->all(), $constraint);
if (!count($errors)) {
//do something
}
}
I see in the comments that you already switched to Sf2 forms. I guess you found the
RepeatedTypefield, which is best for the repeated password field of a registration form – it has a built-in check to verify that the two values match.Your other problem is checking the uniqueness of an email address. Here’s the relevant part of my registration form:
Notes:
$appis injected so I have access to the dependency injection container$app["user"]is my User table via KnpRepositoryServiceProvider$app["user"]->findByEmailreturns null or a user record