【问题标题】:Symfony2 Validator Constraint GreaterThan on other propertySymfony2验证器约束大于其他属性
【发布时间】:2014-02-03 23:56:13
【问题描述】:

我的验证是在一个 yaml 文件中定义的,如下所示;

# src/My/Bundle/Resources/config/validation.yml
My\Bundle\Model\Foo:
    properties:
        id:
            - NotBlank: 
                groups: [add]
        min_time:
            - Range:
                min: 0
                max: 99
                minMessage: "Min time must be greater than {{ limit }}"
                maxMessage: "Min time must be less than {{ limit }}"
                groups: [add]
        max_time:
            - GreaterThan:
                value: min_time
                groups: [add]

如何使用验证器约束 GreaterThan 来检查另一个属性?
例如,确保 max_time 大于 min_time?

我知道我可以创建一个自定义约束验证器,但您当然可以使用GreaterThan 约束来实现。
希望我在这里遗漏了一些非常简单的东西

【问题讨论】:

  • 我不认为这些注解可以使用其他字段。见one of my answer about validating a field depending on another field,实现起来并不复杂。
  • @n.1 我认为这是我必须要做的。如果您将其写为答案,我将接受它(以便其他人可以轻松看到解决方案)
  • 事实上,我注意到使用 simple 注释是不可能的,所以这不是一个好的答案。 :)

标签: php validation symfony


【解决方案1】:

我建议你看看Custom validator,尤其是Class Constraint Validator

我不会复制粘贴整个代码,只是你必须更改的部分。

src/My/Bundle\Model\Foo/Validator/Constraints/CheckTime.php

定义验证器,min_timemax_time 是您要检查的 2 个字段。

<?php

namespace My\Bundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class CheckTime extends Constraint
{
    public $message = 'Max time must be greater than min time';

    public function validatedBy()
    {
            return 'CheckTimeValidator';
    }

    public function getTargets()
    {
            return self::CLASS_CONSTRAINT;
    }
}

src/My/Bundle/Validator/Constraints/CheckTimeValidator.php

定义验证器:

<?php

namespace My\Bundle\Validator\Constraints;

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

class CheckTimeValidator extends ConstraintValidator
{
    public function validate($foo, Constraint $constraint)
    {
            if ($foo->getMinTime() > $foo->getMaxTime()) {
                $this->context->addViolationAt('max_time', $constraint->message, array(), null);
            }
    }
}

src/My/Bundle/Resources/config/validation.yml

使用验证器:

My\Bundle\Entity\Foo:
    constraints:
        - My\Bundle\Validator\Constraints\CheckTime: ~

【讨论】:

    【解决方案2】:

    我建议您使用Callback 验证器。自定义验证器也可以工作,但如果这是一次性验证并且不会在应用程序的其他部分中重复使用,则可能有点过头了。

    使用回调验证器,您可以在同一实体/模​​型上定义它们并在类级别调用。

    你的情况

    // in your My\Bundle\Model\Foo:
    /**
     * @Assert\Callback
     */
    public function checkMaxTimeGreaterThanMin($foo, Constraint $constraint)
    {
            if ($foo->getMinTime() > $foo->getMaxTime()) {
                $this->context->addViolationAt('max_time', $constraint->message, array(), null);
            }
    }
    

    或者如果你使用 YAML

    My\Bundle\Model\Foo:
        constraints:
            - Callback: [checkMaxTimeGreaterThanMin]
    

    【讨论】:

      【解决方案3】:

      使用选项propertyPath 尝试GreaterThan 约束:

      use Symfony\Component\Validator\Constraints as Assert;
      
      /**
       * @ORM\Column(type="datetime", nullable=true)
       * @Assert\DateTime()
       * @Assert\GreaterThan(propertyPath="minTime")
       */
       protected $maxTime;
      

      【讨论】:

        猜你喜欢
        • 2020-10-16
        • 2020-06-04
        • 1970-01-01
        • 2015-03-20
        • 1970-01-01
        • 1970-01-01
        • 2011-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多