gpt4 book ai didi

symfony4 - Symfony 验证在使用自定义操作类时不适用于 Api 平台

转载 作者:行者123 更新时间:2023-12-03 20:11:11 26 4
gpt4 key购买 nike

我正在使用 symfony 4 和 Api Platform 2.5 。我创建了一个自定义操作来更改用户密码。

问题是当我发送 一个空的旧密码 或任何其他必需的属性,验证不起作用。如您所见,我使用了一个名为 的验证组。 “更改密码” .

这是实体用户的一部分:

/**
* @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;

}

这是自定义操作类:
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;

}
}

oldPassword 为空时 dd($erros) 的输出:

enter image description here

没有 dd($erros) 我收到这个错误:

enter image description here

最佳答案

第一

对于 v2.5+ 替换 access_controlsecurity .并替换 access_control_messagesecurity_message .

第二

尝试添加 Default加入你的群组 validation_groups如果您不使用自定义 Controller :

"validation_groups"={"Default", "change_password"},

或添加 Default约束组:
//src/Entity/User.php

@Assert\NotBlank(groups={"Default", "change_password"})

并调用验证:
<?php
// src/Controller/Api/CustomClass.php

declare(strict_types=1);

namespace App\Controller\Api;

use ApiPlatform\Core\Validator\ValidatorInterface;
use App\Entity\User;

class CustomClass extends AbstractController
{
/**
* @var ValidatorInterface
*/
private $validator;

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

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

//other code ...
}
}


你也可以检查这个网址

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

所以解决方案可以是这样的,而无需添加“默认”:
    $errors = $this->validator->validate($data, null, ['change_password']);
if (count($errors) > 0) {
throw new ValidationException($errors);
}

关于symfony4 - Symfony 验证在使用自定义操作类时不适用于 Api 平台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59966119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com