当我提交表单时,尽管我在实体中定义了约束,但未验证包含日期的文本字段。什么是不正确的?我是否需要为包含日期的文本字段编写自定义日期验证器?
在我的表单课中,我有
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('added', 'date', array(
'required' => false,
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
'attr' => array(
'class' => 'datepicker'
)
))
}
在实体中
/**
* @var date
*
* @Assert\Date(message = "test")
* @ORM\Column(name="added", type="date", nullable=true)
*/
private $added;
在控制器中(我需要列出这些错误)
$request = $this->getRequest();
$r = $this->getProfileRepository();
$profile = $id ? $r->find($id) : new \Alden\XyzBundle\Entity\Profile();
/* @var $profile \Alden\XyzBundle\Entity\Profile */
$form = $this->createForm(new ProfileType(), $profile);
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
$errors = $this->get('validator')->validate($profile);
foreach ($errors as $e)
{
/* @var $e \Symfony\Component\Validator\ConstraintViolation */
$errors2[$e->getPropertyPath()] = $e->getMessage();
}
if (count($errors2))
{
...
} else {
$em = $this->getEntityManager();
$em->persist($profile);
$em->flush();
}