问题描述
我正在尝试仅对 api 平台上的自定义操作进行验证,但它不起作用.
我只想在验证没有错误的情况下更改密码,但无论如何都要更改密码.
如果我从验证注释中删除验证组,它会起作用.
例如,如果我将 @Assert\NotBlank(groups={"put-reset-password"}) 替换为 @Assert\NotBlank 验证通过.>
这是实体的代码:
"更改密码"={"方法"="PUT","path"="/users/change-password/{id}",控制器"="App\Controller\ChangePasswordController","access_control"="is_granted('EDIT-PASSWORD', previous_object) and object == user",openapi_context"={摘要"=更改密码"},非规范化_上下文"={组"={put-reset-password"}},"validation_groups"={"put-reset-password"},},
这是我的控制器
public function __invoke(User $data){//验证数据并处理验证错误$this->validator->validate($data);$data->setPassword($this->userPasswordEncoder->encodePassword($data, $data->getNewPassword()));$this->entityManager->flush();$token = $this->tokenManager->create($data);返回 $data;}
这是我使用验证组的属性之一.
/*** @Groups({"put-reset-password"})* @Assert\NotBlank(groups={"put-reset-password"})* @UserPassword(groups={"put-reset-password"})*/私人 $oldPassword;
请问有什么问题吗?
这是奇怪的行为.你得到 null 可能是因为你使用了接口
使用 ApiPlatform\Core\Validator\ValidatorInterface;
如果使用inface use Symfony\Component\Validator\Validator\ValidatorInterface
你必须得到对象ConstraintViolationListInterface
.
我也有类似的问题.以下解决方案对我有帮助:
/*** @Groups({"put-reset-password"})* @Assert\NotBlank(groups={"Default", "put-reset-password"})* @UserPassword(groups={"put-reset-password"})*/私人 $oldPassword;
这很奇怪,但添加 "Default"
后,我的验证在我的自定义操作中起作用.
有用的链接:
https://symfony.com/doc/current/validation/groups.htmlhttps://symfony.com/doc/current/validation/sequence_provider.html
附言抱歉我的英语不好.
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 ?
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.htmlhttps://symfony.com/doc/current/validation/sequence_provider.html
P.S. Sorry for my bad english.
这篇关于在自定义操作控制器中验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!