Answer the question
In order to leave comments, you need to log in
How to add violation for specific CollectionType element?
There is a form in it, a collection, I need to display an error near each element of the collection, I'm trying to add errors to the elements like this. I know that in this code now errors will be added to each element!
$builder->add('projectPings', CollectionType::class, array(
'entry_type' => BillingPingType::class,
'entry_options' => array('label' => false),
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'error_bubbling' => false,
'constraints' => new Callback(array($this, 'validateProjectPings'))
))
public function validateProjectPings($value, ExecutionContextInterface $context)
{
$form = $context->getRoot();
$form_pings = $form->get('projectPings');
$root_path = $form_pings->getPropertyPath()->getElement(0);
foreach ($form_pings as $ping){
$path = $ping->getPropertyPath()->getElement(0);
$context->buildViolation('This field cannot be empty.')
->atPath($root_path)
->atPath(sprintf("[%s]", $path))
->atPath('limitTo')
->addViolation();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question