Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2表单-日期字段类型允许错误的日期_Php_Mysql_Forms_Date_Symfony - Fatal编程技术网

Php Symfony2表单-日期字段类型允许错误的日期

Php Symfony2表单-日期字段类型允许错误的日期,php,mysql,forms,date,symfony,Php,Mysql,Forms,Date,Symfony,我有一个带有日期字段的表单,如下所示: /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name', 'text', array('required' => false, 'label

我有一个带有日期字段的表单,如下所示:

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text', array('required' => false, 'label' => 'name'))
        ->add('phone', 'text', array('required' => false, 'label' => 'phone'))
        ->add('email', 'text', array('required' => false, 'label' => 'email'))

        ->add('nextRevision', 'date', array(
                  'input' => 'datetime', 
                  'widget' => 'single_text', 
                  'format' => 'yyyy-MM-dd', 
                  'required' => false, 
                  'label' => 'nextRevision'
        ))
}
当表单以HTML呈现时,如果我引入无效日期并发送表单,则会显示错误消息“此值无效”。但如果我引入类似20144-03-23的日期,则表单被视为有效,不会显示错误消息,但显示的日期为0002-12-02,并且在MySQL数据库中,日期保存为0000-00-00


如果我在添加字段时指定了格式,为什么会接受此格式?这是Symfony2表单验证器的一个bug吗?有没有一种方法可以在不使用JavaScript的情况下向用户建议此错误?我怎样才能避免这种行为

您似乎没有向该字段添加任何约束。因此,Symfony不进行健全性检查。向该字段添加约束的一种方法如下:

 $builder->add('nextRevision', 'date', array(
    'input' => 'datetime', 
    'widget' => 'single_text', 
    'format' => 'yyyy-MM-dd', 
    'required' => false, 
    'label' => 'nextRevision' , 
    'constraints' => array( new Date() )));
这将检查输入的日期是否与实际日期一致。如果留空,验证将不会生效

请参阅此处的文档:

谢谢你的回答@Debreczeni András。我使用了你在回答中所说的方式,但行为是一样的。无论如何,我认为这个方法应该接受日期,因为,实际上是一个日期。