1

I'm trying to do a validation only on a custom operation on api platform but it doesn't work.

I would like to change the password only if there is no error in the validation but in any case the password is changed.

If I remove the validation groups from the validation annotations it works.

for example if i replace @Assert\NotBlank(groups={"put-reset-password"}) with @Assert\NotBlank the validation pass.

This is the code of the entity :

  "CHANGE-PASSWORD"={
    "method"="PUT",
     "path"="/users/change-password/{id}",
     controller"="App\Controller\ChangePasswordController",
     "access_control"="is_granted('EDIT-PASSWORD', previous_object) and object == user",
     "openapi_context"={
        "summary"="Change Password"
     },
     "denormalization_context"={
        "groups"={"put-reset-password"}
     },
     "validation_groups"={"put-reset-password"},
  },

and here is my controller

public function __invoke(User $data)
{

    // Validate data and handle validation errors
    $this->validator->validate($data);

    $data->setPassword($this->userPasswordEncoder->encodePassword($data, $data->getNewPassword()));


    $this->entityManager->flush();
    $token = $this->tokenManager->create($data);


    return $data;
}

and here is one of my attributes in which i use validation group.

/**
 * @Groups({"put-reset-password"})
 * @Assert\NotBlank(groups={"put-reset-password"})
 * @UserPassword(groups={"put-reset-password"})
 */
private $oldPassword;

Any issue please ?

6
  • 1
    The validator's validate method return a constraints violation list. You have to check the count of violations to know if the data are valid. Commented Dec 30, 2019 at 18:16
  • @FlorianHermann In this case, the constraints violation list return nothing, it seems like when i put groups into assertion of an attribute, the validation fail. Even if i var_dump($this->validator->validate($data)) i have NULL. Commented Dec 30, 2019 at 19:38
  • That seems strange because the validate method is always supposed to return a ConstraintViolationListInterface instance even it the validation is ok. Commented Dec 30, 2019 at 19:52
  • I know but i think that validator doesn't like when im using validations_group inside assertion Commented Dec 31, 2019 at 20:43
  • You mentioned custom operation. Did you try something like this to apply your validation group on your custom operation? api-platform.com/docs/core/validation/… Commented Jan 2, 2020 at 18:17

2 Answers 2

1

This is strange behavior. You get null probably because you use interface

use ApiPlatform\Core\Validator\ValidatorInterface;

If use inface use Symfony\Component\Validator\Validator\ValidatorInterface you must be get object ConstraintViolationListInterface.

I have a similar problem. The following solution helped me:

/**
 * @Groups({"put-reset-password"})
 * @Assert\NotBlank(groups={"Default", "put-reset-password"})
 * @UserPassword(groups={"put-reset-password"})
 */
private $oldPassword;

It's strange, but after added "Default" my validation is worked in my custom operation.

Useful links:

https://symfony.com/doc/current/validation/groups.html https://symfony.com/doc/current/validation/sequence_provider.html

P.S. Sorry for my bad english.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help mate. I have the absolute same case not working.
0

I have the exact same case and I had just kind of made things to work with the Default group, but the actual solution is to give the validator the group you want to validate:

public function __invoke(User $data)
{
    $this->validator->validate($data, null, 'put-reset-password');
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.