Symfony:约束中的验证集不起作用

Symfony: Validation set in Constraints does not work

当我检查更新到 Symfony 2.x-> Symfony 4.4 的应用程序的运行时,我发现验证工作不正常。
即使设置了validation,即使你输入了被validation翻转的值,也不会被翻转,会被保存。
由于 Symfony 更新,这些功能是否有任何变化?

Html5EmailValidator.php

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class Html5EmailValidator extends ConstraintValidator
{
    /**
     * @see http://www.w3.org/TR/html5/forms.html#valid-e-mail-address
     * @see http://momdo.s35.xrea.com/web-html-test/spec/html5-dev/forms.html#valid-e-mail-address
     */
    const PATTERN = '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/';

    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint)
    {
        if ($value === null || $value === '') {
            return;
        }

        if (!preg_match(self::PATTERN, $value, $matches)) {
            $this->context->addViolation($constraint->message);
        }
    }
}

Email.php

use Symfony\Component\Validator\Constraint;

/**
 * E-mail address constraint equivalent to HTML5 type = email check
 */
class Html5Email extends Constraint
{
    public $message = 'This value is not a valid email address.';

    /**
     * {@inheritdoc}
     */
    public function validatedBy()
    {
        return 'Html5Email';
    }
}

services.yaml

    common.validator.html5EmailValidator:
        class: AppBundle\Validator\Constraints\Html5EmailValidator
        tags:
            - { name: validator.constraint_validator, alias: Html5Email }

Staff.php

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use AppBundle\Model\Lib\Parameters;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Model\Repository\StaffRepository")
 * @ORM\Table(name="staff", uniqueConstraints={
 *      @ORM\UniqueConstraint(name="idx_staff_unique", columns={"staff_login_id"}),
 *      @ORM\UniqueConstraint(name="idx_image_mail_unique", columns={"image_mail"})
 * })
 * @ORM\HasLifecycleCallbacks
 */
class Staff implements AdvancedUserInterface, \Serializable, EquatableInterface
{
    /**
     * 
     * @ORM\Column(name="staff_mail", type="string", length=255, nullable=true, options={
     *      "comment"="staffmailaddress"
     * })
     */
    protected $staffMail;
    /**
     * Set staffMail
     *
     * @param string $staffMail
     * @return Staff
     */
    public function setStaffMail($staffMail)
    {
        $this->staffMail = $staffMail;

        return $this;
    }

    /**
     * Get staffMail
     *
     * @return string
     */
    public function getStaffMail()
    {
        return $this->staffMail;
    }

validation.yaml

AppBundle\Model\Entity\Staff:
    constraints:
        - AppBundle\Validator\Constraints\Staff:
            groups: [hqNew, hqEdit]
    getters:
        staffMail:
            - Length: { max: 200, groups: [hqNew, hqInitialSetting, hqEdit] }
            - AppBundle\Validator\Constraints\Html5Email:
                groups: [hqNew, hqInitialSetting, hqEdit]

试过错误 Html5Email.php

  /**
   * @Annotation
   */
  class Html5Email extends Constraint
  {
      public $message = 'This value is not a valid email address.';
  
     /**
      * {@inheritdoc}
      */
      public function validatedBy()
      {
          return 'AppBundle\Validator\Constraints\Html5EmailValidator';
      }
  }

Staff.php

use AppBundle\Validator\Constraints\Html5Email as Html5Email;

      /*
       * @Html5Email
       * @ORM\Column(name="staff_mail", type="string", length=255, nullabl     e=true, options={
       *      "comment"="staffemailaddress"
       * })
       */
      protected $staffMail;

我改成下面的形式就解决了这个问题。 Html5EmailValidator.php

        if (!preg_match(self::PATTERN, $value, $matches)) {
            $this->context->buildViolation($constraint->message)
                          ->addViolation();
        }