I'm using symfony 4 and Api Platform 2.5 . I have created a custom operation to change user password.
The problem is that when I send an empty oldPassword or any other required attributes,the validation doesn't work. As you can see, I used a validation group named "change_password".
This is a part of Entity User :
/**
* @ApiResource(
* itemOperations={
* "user_change_password"={
* "method"="PUT",
* "path"="/users/change-password/{id}",
* "controller"=UserChangePassword::class,
* "formats"={"json"},
* "denormalization_context"={"groups"={"user:change-password"}},
* "normalization_context"={"groups"={"user:read"}},
* "validation_groups"={"change_password"},
* "access_control"="is_granted('EDIT', object)",
* "access_control_message"="access denied"
* },
* },
* )
*
*/
class User implements UserInterface, \Serializable, EquatableInterface
{
/**
* @Assert\NotBlank(
* message=Tthis value should not be blank",
* groups={"change_password"}
* )
* @SecurityAssert\UserPassword(
* message = "Wrong value for your current password",
* groups={"change_password"}
* )
* @Groups({"user:change-password"})
*/
private $oldPassword;
/**
* @Assert\NotBlank(
* message="This value should not be blank",
* groups={"registration", "change_password"}
* )
* @Assert\Length(
* min="6",
* max="32",
* minMessage="Password must be at least ({{ limit }}) characters.",
* maxMessage="Password must have a maximum of ({{ limit }}) characters.",
* groups={"registration", "change_password"}
* )
* @Groups({"user:write", "user:change-password"})
*/
private $plainPassword;
/**
* @Assert\NotBlank(
* message="This value should not be blank",
* groups={"registration", "change_password"}
* )
* @Assert\Expression(
* "this.getPlainPassword() == this.getRetypedPlainPassword()",
* groups={"registration", "change_password"}
* )
* @Groups({"user:write", "user:change-password"})
*/
private $retypedPlainPassword;
}
This is the custom operation class :
class UserChangePassword
{
public function __invoke(User $data)
{
$errors = $this->validator->validate($data);
if (count($errors) > 0) {
throw new ValidationException($errors);
}
dd($errors) ;
$oldPassword = $data->getOldPassword();
if ($this->passwordEncoder->isPasswordValid($data, $oldPassword)) {
// do changes
}
return $data;
}
}
The output of dd($erros) when the oldPassword is empty :
And without dd($erros) I get this error :