【发布时间】:2016-03-03 08:10:37
【问题描述】:
我在 Symfony 2.7.10 中有一个表单,其定义如下所示:
<?php
// ...
class RecipeType extends AbstractType
{
// ...
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('meal_schema', 'choice', [
'label' => 'Mealtime schema',
'choices' => [
'breakfast' => 'Breakfast',
'second_breakfast' => 'Second breakfast',
'lunch' => 'Lunch',
'dinner' => 'Dinner',
'snack' => 'Snack',
'supper' => 'Supper',
],
'multiple' => true,
'expanded' => true,
'label_attr' => ['class' => 'col-md-2'],
'required' => true,
])
}
// ...
}
这是validation.yml文件中验证的样子:
My\Real\Namespace\Entity\Recipe:
properties:
name:
- NotBlank: ~
mealSchema:
# Look below
名称字段验证有效,但mealSchema 验证无效。 我已经尝试过但没有成功的设置:
# This one works but assigns error to form instead of field so it's displayed on top of the page
- NotBlank:
message: Mealtime schema should not be blank
# This doesn't work
- Choice:
callback: [RecipeMealSchemaChoices, getChoiceKeys] # This method isn't even called
min: 1
minMessage: "Please select meal schema"
# This also doesn't work
- Count:
min: 1
max: 99
minMessage: Mealtime schema should not be blank
maxMessage: Max meal time exceeded
【问题讨论】:
-
stackoverflow.com/questions/19818548/… 这看起来很有希望,但看起来有很多不必要的工作。
-
验证中的属性名称应与表单中的字段名称相同 (
mealSchema !== meal_schema)。
标签: php forms validation symfony choice