【问题标题】:Custom Validators Symfony2 ordering自定义验证器 Symfony2 排序
【发布时间】:2013-02-12 08:19:52
【问题描述】:

我有一个带有一些自定义验证器的实体,如下所示:

use Digital\ApplicationBundle\Validator\Constraints\ConstrainsUsername;
use Digital\ApplicationBundle\Validator\Constraints\ConstrainsProduct;
use Digital\ApplicationBundle\Validator\Constraints\ConstrainsGiftValid;

/**
 * @DigitalAssert\ConstrainsGiftValid
 */
class Gift
{

/**
 * @DigitalAssert\ConstrainsUsername
 */
private $username;

/**
 * @DigitalAssert\ConstrainsProduct
 */
private $productName;
[...]

我的问题是如何设置检查顺序......

我想先验证我的属性,如果属性有效,那么我想检查这两个属性是否允许“存在”在一起...

所以我的案例需要验证器的特定顺序。

请问如何实现?

当前的问题是我的班级验证“ConstrainsGiftValid”在他们之前开始;S

非常感谢任何帮助。

【问题讨论】:

    标签: forms validation symfony symfony-2.1


    【解决方案1】:

    要检查$username$productName 是否一起使用,您必须创建一个custom validation constraint

    如果您需要验证器的特定顺序,并希望稍后在代码中重用这两个字段的验证,我认为应该这样做:

    1 为 {username and productName} 创建一个表单类型。

    2 在该 formType 上应用您的验证规则。如果您想使用验证顺序,则需要在这种特殊情况下对整个表单应用约束。因此,您只能根据需要并按照您想要的顺序抛出错误。

    3您终于可以将formType 嵌入到您的GiftFormType 中。不要忘记使用 Valid 约束或将 cascade_validation 选项设置为 true 来验证嵌入的表单。

    【讨论】:

    • 嗨 Patt 我有 3 个自定义验证约束...问题是我需要以某种方式订购它们...优先考虑..
    • 如果您有 3 个自定义验证约束,验证器将验证所有约束,因此顺序无关紧要。
    • 嗯......所以你说的是在我的班级约束中处理所有事情而不是让他们分裂?这实际上是有道理的...我可以这样验证...但是如果这只是 1 个属性失败,则错误不会显示在每个属性旁边...
    • 我还没有尝试过,但您似乎可以使用addViolationAtPath()addViolationAt 将违规添加到一个特定字段,而不是验证器中使用的经典addViolation()。见here。如果你试试这个,你能告诉我这是否有效吗?祝你好运。
    • 如果你想在某处重复使用验证,你可以为 {username and productName} 创建一个表单类型。然后在该 formType 上应用您的验证规则。您最终可以将该 formType 嵌入到您的 GiftFormType 中。不要忘记使用 Valid 约束或将 cascade_validation 选项设置为 true 来验证嵌入的表单。
    【解决方案2】:

    好的,在一个约束中处理所有内容。

    这包括对特定属性的绑定错误和针对不同故障的不同消息:

    public function isValid($gift, Constraint $constraint)
    {
    
        // Validate product.
        /** @var $product Product */
        $product = $this->em->getRepository('DigitalApplicationBundle:Shop\Product')->findOneBy(array('name' => $gift->getProductName()));
        if (!$product instanceof Product) {
    
            $this->context->addViolationAtSubPath('username', $constraint->messageProduct, array('%string%' => $gift->getProductName()), null);
            return false;
    
        }
    
        // Validate user.
        /** @var $user User */
        $user = $this->em->getRepository('DigitalUserBundle:User')->findOneBy(array('username' => $gift->getUsername()));
        if (!$user instanceof User) {
    
            $this->context->addViolationAtSubPath('username', $constraint->messageUser, array('%string%' => $gift->getUsername()), null);
            return false;
        }
    
    
        // Gift correct type of the item!
        if (($product->getType() != 0) && ($user->getGender() !== $product->getType())) {
    
            $this->context->addViolationAtSubPath('username', $constraint->messageType, array('%string%' => $gift->getProductName()), null);
            return false;
    
        }
    
        // If already owning this product.
        foreach ($user->getWardrobe()->getProducts() as $wardrobeProduct) {
            if ($product == $wardrobeProduct) {
    
                $this->context->addViolationAtSubPath('username', $constraint->message, array('%string%' => $gift->getProductName(), '%user%' => $gift->getUsername()), null);
                return false;
            }
        }
    
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      相关资源
      最近更新 更多