Php 如何在Symfony表单约束中设置有效日期或空字段
如何使用约束来验证日期,以允许日期采用yyyy mm dd格式,或在此代码中保留日期为空Php 如何在Symfony表单约束中设置有效日期或空字段,php,forms,symfony,date,constraints,Php,Forms,Symfony,Date,Constraints,如何使用约束来验证日期,以允许日期采用yyyy mm dd格式,或在此代码中保留日期为空 use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints as Assert; class ArticleType extends AbstractType { public functi
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;
class ArticleType extends AbstractType {
public function getName() {
return 'article';
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('date', 'date', array(
'label' => 'Date',
'input' => 'string',
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'html5' => false,
'constraints' => array(
new Assert\NotBlank(),
new Assert\Date(),
),
))
}
}
是否可以在不创建自定义验证器的情况下进行验证?默认情况下,所有验证器都允许空值。只需删除NotBlank断言,它就可以工作了。我已经删除了“new assert\NotBlank()”并得到了“警告:IntlDateFormatter::parse():日期解析失败”。它必须是“文本”字段类型,而不是“日期”。