Symfony 表单验证约束表达式

Symfony Form Validation Constraint Expression

我有表单,需要创建内联验证:

$builder
        ->add('Count1', 'integer', [
            'data'        => 1,
            'constraints' => [
                new NotBlank(),
                new NotNull(),
            ],
        ])
        ->add('Count2', 'integer', [
            'constraints' => [
                new NotBlank(),
                new NotNull(),
            ],
        ])
        ->add('Count3', 'integer', [
            'data'        => 0,
            'constraints' => [
                new NotBlank(),
                new NotNull(),
            ],
        ])

如何为规则的白色内联验证表达式

  1. 计数 2 >=计数 1
  2. Count3 <=Count2
  3. Count2 >= $someVariable

您可以利用 CallbackValidator (docs):

在您的情况下,为了再次验证一个字段,您需要向表单类型而不是字段添加约束:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'constraints' => array(
            new Assert\Callback(function($data){
                // $data is instance of object (or array) with all properties
                // you can compare Count1, Count2 and Count 3 
                // and raise validation errors
            });
        )
    ));
}

如果您不想在 setDefaultOptions 中设置,您也可以在创建表单时传递 constraints 选项。

从最简单的开始

3) Count2 >= $someVariable

    ->add('Count3', 'integer', [
        'data'        => 0,
        'constraints' => [
            new NotBlank(),
            new NotNull(),
            new GreaterThanOrEqual($someVariable),
        ],
    ])

1) 至于前两个,您必须为 class 范围而不是 属性 范围实施约束。并为整个表单分配这些约束

public function buildForm(FormBuilderInterface $builder, array $options)
{
   $builder
    ->add('Count1', 'integer', [
        'data'        => 1,
        'constraints' => [
            new NotBlank(),
            new NotNull(),
        ],
    ])
    ->add('Count2', 'integer', [
        'constraints' => [
            new NotBlank(),
            new NotNull(),
        ],
    ])
    ->add('Count3', 'integer', [
        'data'        => 0,
        'constraints' => [
            new NotBlank(),
            new NotNull(),
        ],
    ])
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
     $resolver->setDefaults(['constraints' => [
            new YourCustomConstraint(),
          ]]);
}

如何实现验证器,见the documentation。 但是在你的YourCustomConstraintValidator中你会有类似

的东西
public function validate($value, Constraint $constraint)
{
     if ($value->getCount1() > $value->getCount2() {
           $this->context->addViolation(...);
     }
}

针对案例 1 和案例 2 使用 Expression Constraint 的其他解决方案。

use Symfony\Component\Validator\Constraints as Assert;

// ...

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'constraints' => [
            new Assert\Expression([
                'expression' => 'value["Count2"] >= value["Count1"]',
                'message' => 'count2 must be greater than or equal to count1'
            ]),
            new Assert\Expression([
                'expression' => 'value["Count3"] <= value["Count2"]',
                'message' => 'count3 must be less than or equal to count2'
            ]),
        ],
    ]);
}

对于情况 3,您可以直接在 Count2 字段上使用 Assert\GreaterThanOrEqual 约束。

I guess your form doesn't have a binding object model, otherwise to read the documentation referred is enough and better because you could use these expression on your properties directly.

我在使用 Symfony 的表达式比较两个日期时遇到了一些问题。

这是有效的代码:

 $builder->add(
            'end',
            DateTimeType::class,
            [
                'label' => 'Campaign Ends At',
                'data' => $entity->getEnd(),
                'required' => true,
                'disabled' => $disabled,
                'widget'  => 'single_text',
                'constraints' => [
                    new Assert\GreaterThan(['value' => 'today']),
                    new Assert\Expression(
                                [
                                    //here end and start are the name of two fields
                                    'expression' => 'value > this.getParent()["start"].getData()',
                                    'message' => 'my.form.error.end.date.bigger.than.start'
                                ]
                            ),
                ]

            ]
        );