Add error to symfony 2 form element

I am checking some validation in my controller. And I want to add an error to a specific element of my form on error. My form:

use Symfony\Component\Form\FormError; // ... $config = new Config(); $form = $this->createFormBuilder($config) ->add('googleMapKey', 'text', array('label' => 'Google Map key')) ->add('locationRadius', 'text', array('label' => 'Location radius (km)')) ->getForm(); // ... $form->addError(new FormError('error message')); 

The addError () method adds an error to the form, not to the element. How to add error to locationRadius element?

+67
php validation symfony symfony-forms
Sep 14
source share
2 answers

You can do

 $form->get('locationRadius')->addError(new FormError('error message')); 

Like form elements, they are also of type FormInterface .

+139
Sep 14 '12 at 7:07
source share
— -

OK guys, I have a different way. This is more complicated and only for specific cases.

My case:

I have a form and after submitting I submit the data to the API server. And the errors I received from the API server, as well.

API server error format:

 array( 'message' => 'Invalid postal code', 'propertyPath' => 'businessAdress.postalCode', ) 

My goal is to get a flexible solution. Allows you to set an error for the corresponding field.

 $vm = new ViolationMapper(); // Format should be: children[businessAddress].children[postalCode] $error['propertyPath'] = 'children['. str_replace('.', '].children[', $error['propertyPath']) .']'; // Convert error to violation. $constraint = new ConstraintViolation( $error['message'], $error['message'], array(), '', $error['propertyPath'], null ); $vm->mapViolation($constraint, $form); 

What is it!

NOTE! addError() bypasses the error_mapping option.




My form (address form embedded in the Company form):

Company

 <?php namespace Acme\DemoBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints; class Company extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('companyName', 'text', array( 'label' => 'Company name', 'constraints' => array( new Constraints\NotBlank() ), ) ) ->add('businessAddress', new Address(), array( 'label' => 'Business address', ) ) ->add('update', 'submit', array( 'label' => 'Update', ) ) ; } public function getName() { return null; } } 

Address

 <?php namespace Acme\DemoBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints; class Address extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder // ... ->add('postalCode', 'text', array( 'label' => 'Postal code', 'constraints' => array( new Constraints\NotBlank() ), ) ) ->add('town', 'text', array( 'label' => 'Town', 'constraints' => array( new Constraints\NotBlank() ), ) ) ->add('country', 'choice', array( 'label' => 'Country', 'choices' => $this->getCountries(), 'empty_value' => 'Select...', 'constraints' => array( new Constraints\NotBlank() ), ) ) ; } public function getName() { return null; } } 
+4
Feb 11 '14 at
source share



All Articles