I have a change password form with 3 fields:
currentPasword
newPassword
confirmNewPassword
and I want to validate that the newPassword
is not equal the current one.
I can validate to check it's not empty in my ChangePassword
entity that the form uses, for example, using:
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('newPassword', new NotBlank());
}
But how can I validate one field against another? Do I just need to hard code that and if so, where would that code best sit?
I would add a validator constraint to your user entity. That way you are able to compare the currentPassword with newPassword. And of course newPassword vs confirmNewPassword.
Example:
// src/Entity/Authentification/User.php
namespace App\Entity\Authentification;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class User
{
// here you can add assert annotations (alternatively to your loadValidatorMetaData)
/**
* @Assert\NotBlank(„Please enter current password“)
*/
private currentPassword;
private newPassword;
private confirmNewPassword;
//getter & setter go here
/**
* @Assert\Callback
*/
public function validate(ExecutionContextInterface $context, $payload)
{
if ($this->getNewPasswort() === $this->getCurrentPassword()) {
$context->buildViolation('Your new password must be different from the current Password!')
->atPath('newPassword')
->addViolation();
}
if ($this->getNewPasswort() ==! $this->getConfirmNewPassword()) {
$context->buildViolation('Your confirmed password is not equal to the new password!')
->atPath('confirmNewPassword')
->addViolation();
}
}
}
With this custom validations youre able to validate between more than one field. But remember, that this validation is fired after you submit your form. The AssertCallback is triggered by use of $form->isValid() in your controller:
if($form->isSubmitted() && $form->isValid()).
Because of the violation you can catch failed validation with:
if($form->isSubmitted() && !$form->isValid())
You can handle the user feedback of the violation in your formType and the html outputs. (Therefore have a look at Symfony2 : How to get form validation errors after binding the request to the form)
References:
https://symfony.com/doc/current/validation/custom_constraint.html https://symfony.com/doc/current/reference/constraints/Callback.html
Hope it helps :)