自定义操作控制器中的验证 [英] Validation in custom operation controller

查看:35
本文介绍了自定义操作控制器中的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅对 api 平台上的自定义操作进行验证,但它不起作用.

我只想在验证没有错误的情况下更改密码,但无论如何都要更改密码.

如果我从验证注释中删除验证组,它会起作用.

例如,如果我将 @AssertNotBlank(groups={"put-reset-password"}) 替换为 @AssertNotBlank 验证通过.>

这是实体的代码:

 "更改密码"={"方法"="PUT","path"="/users/change-password/{id}",控制器"="AppControllerChangePasswordController","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"})* @AssertNotBlank(groups={"put-reset-password"})* @UserPassword(groups={"put-reset-password"})*/私人 $oldPassword;

请问有什么问题吗?

解决方案

这是奇怪的行为.你得到 null 可能是因为你使用了接口

使用 ApiPlatformCoreValidatorValidatorInterface;

如果使用inface use SymfonyComponentValidatorValidatorValidatorInterface 你必须得到对象ConstraintViolationListInterface.

我也有类似的问题.以下解决方案对我有帮助:

/*** @Groups({"put-reset-password"})* @AssertNotBlank(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 @AssertNotBlank(groups={"put-reset-password"}) with @AssertNotBlank the validation pass.

This is the code of the entity :

  "CHANGE-PASSWORD"={
    "method"="PUT",
     "path"="/users/change-password/{id}",
     controller"="AppControllerChangePasswordController",
     "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"})
 * @AssertNotBlank(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 ApiPlatformCoreValidatorValidatorInterface;

If use inface use SymfonyComponentValidatorValidatorValidatorInterface you must be get object ConstraintViolationListInterface.

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

/**
 * @Groups({"put-reset-password"})
 * @AssertNotBlank(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.

这篇关于自定义操作控制器中的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆