我有表格,需要创建内联验证:
$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(),
],
])
规则的白色内联验证表达式
最佳答案
使用 Expression Constraint 的其他解决方案对于情况 1 和 2。
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,您可以使用
Assert\GreaterThanOrEqual
直接约束 Count2
field 。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.
关于validation - Symfony 表单验证约束表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40287266/