首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义控制器验证不起作用- Api-platform

自定义控制器验证不起作用- Api-platform
EN

Stack Overflow用户
提问于 2020-10-11 05:23:23
回答 1查看 503关注 0票数 0

您好,我正在尝试验证用户实体验证,但只有一个带有补丁请求的属性,但我得到了Cannot validate values of type \"NULL\" automatically. Please provide a constraint.

即使从validation_groups中删除默认设置,也会得到相同的结果

非常感谢您的帮助。

应用\实体\用户

代码语言:javascript
复制
     /**
         * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
         * @ApiResource(
         *     itemOperations={
         *          "get",
         *          "put" = {
         *              "method" = "PUT",
         *              "security"="(is_granted('ROLE_ADMIN') and object.getAssociation() == user.getAssociation())",
         *              "security_message"="Only Admin or the owner can edit this field",
         *              "denormalization_context"={"groups"={"user:update"}, "disable_type_enforcement"=true},
         *              "validation_groups"={"Default", "user_update"}
         *          },
         *          "patch_add_new_member_to_pay" = {
         *              "method" = "PATCH",
         *              "path" = "/users/{id}/addNewMemberToPay",
         *              "controller" = PayingMembershipForOthersController::class,
         *              "denormalization_context" = {"groups" = {"add_new_member_to_pay"}},
         *              "validation_groups" = {"Default", "add_new_member_to_pay"}
         *          }
         *      },
         *     collectionOperations={
         *          "get",
         *          "post" = {
         *               "method" = "POST",
         *               "security"="is_granted('ROLE_ADMIN')",
         *               "denormalization_context"={"groups"={"user:create"}, "disable_type_enforcement"=true},
         *               "validation_groups"={"Default", "user_create"}
         *          },
         *      },
         *      normalizationContext={"groups"={"user:read"}}
         * )
         */

    
    class User {
       /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         *
         * @Groups({"user:read"})
         */
        private ?int $id = null;

       /**
         * @ORM\Column(type="string", length=100)
         * @Assert\NotBlank(groups={"user_create", "user_update"})
         * @Assert\Regex(pattern="/\d/", match=false, message="Your name should not contain numbers")
         * @Groups({"user:read", "user:create", "user:update"})
         */
        private ?string $fullname;
    
       /**
         * @Groups({"add_new_member_to_pay"})
         * @Assert\NotNull(groups={"add_new_member_to_pay"})
         */
        private ?string $payForNewMember = null;
        
        ...getters and setters
   }

App\Controller\PayingMembershipForOthersController

代码语言:javascript
复制
namespace App\Controller;

use ApiPlatform\Core\Validator\ValidatorInterface;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;

class PayingMembershipForOthersController
{

    private ValidatorInterface $validator;

    public function __construct(ValidatorInterface $validator)
    {
        $this->validator = $validator;
    }

    public function __invoke(User $data, Request $request)
    {
        $this->validator->validate($data);
    }

}

所以我的目的是设置空值{"payForNewMember": ""}验证不显示违规列表

EN

回答 1

Stack Overflow用户

发布于 2020-10-13 14:21:42

您的自定义控制器必须返回用户或响应。

代码语言:javascript
复制
class PayingMembershipForOthersController
{

    private ValidatorInterface $validator;

    public function __construct(ValidatorInterface $validator)
    {
        $this->validator = $validator;
    }

    public function __invoke(User $data)
    {
        $this->validator->validate($data);
        return $data;
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64298433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档