M
M
Maxim Lagoysky2019-07-02 13:14:14
symfony
Maxim Lagoysky, 2019-07-02 13:14:14

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();
        }
    }

5d1b2de28eee8631170561.png
But the error is still added to projectPings, in general, in cause, the path of the error seems to be correctly indicated, maybe I just don’t know how to display it in the template now?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kafkiansky, 2019-07-02
@mad_maximus

https://symfony.com/doc/current/reference/constrai...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question