Forms 自定义验证器Symfony2订购

Forms 自定义验证器Symfony2订购,forms,validation,symfony,symfony-2.1,Forms,Validation,Symfony,Symfony 2.1,我有一个实体,其中包含一些自定义验证器,如: use Digital\ApplicationBundle\Validator\Constraints\ConstrainsUsername; use Digital\ApplicationBundle\Validator\Constraints\ConstrainsProduct; use Digital\ApplicationBundle\Validator\Constraints\ConstrainsGiftValid; /** * @Di

我有一个实体,其中包含一些自定义验证器,如:

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;
[...]
我的问题是如何设置检查顺序

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

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

请问如何才能做到这一点

当前的问题是我的类验证“ConstraintsGiftValid”在它们之前生效;


非常感谢您的帮助。

要检查
$username
$productName
是否同时使用,您必须创建一个

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

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

2在该表单类型上应用验证规则。在这种特殊情况下,如果您想处理验证顺序,则需要对整个表单应用约束。因此,只有在您愿意的情况下,才能按照您想要的顺序抛出错误


3您最终可以将该
formType
嵌入到您的
GiftFormType
中。不要忘记使用有效约束或将
cascade\u validation
选项设置为
true
,以验证嵌入表单。

好,将所有内容都处理在一个约束中即可

这包括将错误绑定到特定属性,以及针对不同的故障错误不同的消息:

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;
}

嗨,帕特,我有我的3个自定义验证约束。。。问题是我需要以某种方式订购它们。。。如果您有3个自定义验证约束,验证器将验证所有约束,因此顺序无关紧要。嗯。。。所以你的意思是处理我的类约束中的所有内容,而不是将它们拆分??这实际上是有道理的。。。我可以通过这种方式验证这一点。。。但是,如果只有一个属性失败,那么错误不会显示在每个属性旁边……我还没有尝试过,但似乎可以使用
addViolationAtPath()
addViolationAt
而不是验证程序中使用的经典
addViolationAt()
将冲突添加到一个特定字段中。看见如果你试试这个,你能告诉我是否有效吗?祝你好运。如果你想在某处重用验证,你可以为{username and productName}创建一个表单类型。然后在该表单类型上应用验证规则。您最终可以将该formType嵌入到GiftFormType中。不要忘记使用有效约束或将
cascade\u validation
选项设置为
true
以验证嵌入表单。