为了账号安全,请及时绑定邮箱和手机立即绑定

Symfony 4 自定义验证器约束未加载

Symfony 4 自定义验证器约束未加载

PHP
繁星淼淼 2022-10-28 15:20:50
我正在尝试在https://symfony.com/doc/current/validation/custom_constraint.html中描述的 Symfony 4.4 项目中创建自定义验证器我添加了下一个文件:src/Validator/Constraints/PhoneNumber.php<?phpnamespace App\Validator\Constraints;use Symfony\Component\Validator\Constraint;/** * @Annotation */class PhoneNumber extends Constraint{    public $message = 'The string contains an illegal character: it can only contain letters or numbers.';}src/Validator/Constraints/PhoneNumberValidator.phpnamespace App\Validator\Constraints;use Symfony\Component\Validator\Constraint;use Symfony\Component\Validator\ConstraintValidator;use Symfony\Component\Validator\Exception\UnexpectedTypeException;use Symfony\Component\Validator\Exception\UnexpectedValueException;class PhoneNumberValidator extends ConstraintValidator{    public function validate($value, Constraint $constraint)    {        dd('er wordt gevalideerd!');        if (!$constraint instanceof PhoneNumber) {            throw new UnexpectedTypeException($constraint, PhoneNumber::class);        }        // custom constraints should ignore null and empty values to allow        // other constraints (NotBlank, NotNull, etc.) take care of that        if (null === $value || '' === $value) {            return;        }        if (!is_string($value)) {            // throw this exception if your validator cannot handle the passed type so that it can be marked as invalid            throw new UnexpectedValueException($value, 'string');            // separate multiple types using pipes            // throw new UnexpectedValueException($value, 'string|int');        }        if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) {            // the argument must be a string or an object implementing __toString()            $this->context->buildViolation($constraint->message)                ->setParameter('{{ string }}', $value)                ->addViolation();        }    }}在我尝试了上述方法之后,验证器没有响应......所以接下来我尝试将验证器约束添加到我的实体联系人中。这也行不通。有人看到我做错了什么或对我可以尝试什么有建议?提前致谢!
查看完整描述

2 回答

?
BIG阳

TA贡献1859条经验 获得超6个赞

您正在寻找的约束是否可能是从相关实体中设置的?如果是这样,则尝试将验证从第一个实体传递到第二个实体。


这可以通过以下方式完成:


@Assert\Valid()

不要忘记使用它:


use Symfony\Component\Validator\Constraints as Assert; 

class Contact

{


    .....



    /**

     * @ORM\Column(type="string", length=255, nullable=true)

     * @CustomAssert\PhoneNumber

     */

    private $phoneNumber;

    

    

    

class Person

{


    .....



    /**

     * @ORM\OneToOne(targetEntity="App\Entity\Contact")

     * @Assert\Valid()

     */

    private $contact;


查看完整回答
反对 回复 2022-10-28
?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

您可以尝试 在您的类中实现该validateBy方法。PhoneNumber在正常情况下,您不需要这样做,因为默认行为是查找以Validator( 将是PhoneNumberValidator) 为后缀的相同约束名称。

另一个原因可能是validate调用ValidatorInterface. 也许您正在传递一些验证组(如果您可以在此处与我们分享该部分),在这种情况下,您需要在注释中指定@CustomAssert\PhoneNumber(groups={"some_group"})


查看完整回答
反对 回复 2022-10-28
  • 2 回答
  • 0 关注
  • 127 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号