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

问题描述 投票:0回答:2

我正在尝试在https://symfony.com/doc/current/validation/custom_constraint.html中描述的Symfony 4.4项目中创建自定义验证程序>

我添加了以下文件:

src / Validator / Constraints / PhoneNumber.php

<?php

namespace 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.php

namespace 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();
        }
    }
}

config / validator / validation.yaml

App\Entity\AcmeEntity:
    properties:
        name:
            - NotBlank: ~
            - App\Validator\Constraints\ContainsAlphanumeric: ~

在我尝试了上述验证后,验证器没有响应...因此,我接下来尝试将验证器约束添加到我的实体联系人中。

src / Entity / Contact.php

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

use App\Validator\Constraints as CustomAssert ;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\ContactRepository")
 */
class Contact
{

    .....


    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @CustomAssert\PhoneNumber
     */
    private $phoneNumber;

这也不起作用。

有人看到我做错了事或对我可以尝试的事提出建议?预先感谢!

[我正在尝试在https://symfony.com/doc/current/validation/custom_constraint.html中描述的Symfony 4.4项目中创建自定义验证程序,我添加了下一个文件:src / Validator / ...] >

php symfony validation constraints
2个回答
0
投票

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

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.