我在集合中使用自定义类验证器时遇到问题。它发现错误,获取良好的属性路径,但将其发送到父表单。它导致错误列在父表单中,而不是集合的每个子表单中......
一些更相关的代码:
验证.yml
My\SuperBundle\Entity\Event:
properties:
conflictComment:
- NotNull: ~ # this property constraint got the right property path in the right form !
constraints:
- My\SuperBundle\Validator\DateIsAvailable: ~ # this one got the right property path, but in the parent form.
MultiEventType.php,得到子表单错误的父表单
$builder
->add('events', 'collection', array(
'type' => new \My\SuperBundle\Form\Type\EventDateType()
));
...
'data_class' => 'My\SuperBundle\Form\Model\MultiEvent'
EventDateType.php,应该得到错误的集合
$required = false;
$builder
->add('begin', 'datetime', array(
'date_widget' => 'single_text',
'time_widget' => 'text',
'date_format' => 'dd/MM/yyyy',
'label' => 'form.date.begin')
)
->add('automatic', 'checkbox', compact('required'))
->add('conflictComment', 'textarea', compact('required'));
...
'data_class' => '\My\SuperBundle\Entity\Event'
DateIsAvailableValidator.php
namespace My\SuperBundle\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class DateIsAvailableValidator extends ConstraintValidator
{
private $container;
public function __construct($container)
{
$this->container = $container;
}
public function isValid($event, Constraint $constraint)
{
$this->setMessage($constraint->message);
return false;
}
}
属性路径就像children[events][0].data但呈现在form_errors(multiEventForm.events)上,而不是form_errors(prototype),因为它应该在我的集合行模板中......
有没有办法以正确的形式获取错误,或者这是一个错误?