格式列仅对datetime
,date
和choice
类型设计的。
对于datetime
和date
它表示日期格式,如Y-m-d H:i:s
和choice
的选项阵列。
我还没有找到任何有关它的文档,所以我不得不查看源代码。这是FormTypeParser类,其中FormType
实际上已被解析,并且您可以看到如何设置格式字段。
在FormTypeParserTest类,你可以看到如何使用它。只需传递字符串参数format
名称作为其中一种可用类型,解析器将处理它。
更新:你要在你的FormType
类中定义你的约束。
例如:
class TestType extends AbstractType
{
/**
* @Assert\Type("string")
* @Assert\Length(min="10", max="255")
* @Assert\Regex("/^[^<>]+$/i")
*/
private $title;
/**
* @Assert\Type("string")
* @Assert\Length(min="10", max="255")
* @Assert\Regex("/^[^<>]+$/i")
*/
private $content;
/**
* @Assert\Date()
*/
private $created;
public function getName()
{
return 'test';
}
}
将被分解成:
在 doParse()方法
ValidationParser
发现您的FormType
类中定义的所有约束,然后执行为他们每个人parseConstraint()
方法。
您也可以按照上面所述使用FormTypeParser
。例如:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('created', 'date', array('label' => 'Created', 'format' => 'yyyy-MM-dd'))
->add('color', 'choice', array('label' => 'Color', 'choices' => array('grey' => '#CCCCCC', 'red' => '#FF0000')))
->add('save', 'submit');
}
会被解析为:

希望它现在帮助!
我没有想写入筛选的手,我会用我的形式输入和nelmio阅读形式与实体验证,并使用这些作为格式参数 – ghanbari