How to add validators on the fly in Symfony2?

I have a password form field ( not mapped to the User password) that will be used in the password form to change, as well as two other (displayed) fields, first and last .

I have to add validators on the fly: if the password is empty, then there should be no confirmation . Otherwise, the new MinLength and MaxLength validators should be added.

Here's what I have done so far: create a repeating password field, add CallbackValidator and return if $form->getData() is null .

Then, how can I add validators for minimum and maximum length in the $ field?

  $builder = $this->createFormBuilder($user); $field = $builder->create('new_password', 'repeated', array( 'type' => 'password', 'first_name' => 'Password', 'second_name' => 'Confirm password', 'required' => false, 'property_path' => false // Not mapped to the entity password )); // Add a callback validator the the password field $field->addValidator(new Form\CallbackValidator(function($form) { $data = $form->getData(); if(is_null($data)) return; // Field is blank // Here password is provided and match confirm, check min = 3 max = 10 })); // Add fields to the form $form = $builder ->add('first', 'text', array('required' => false)) // Mapped ->add('last', 'text', array('required' => false)) // Mapped ->add($field) // Not mapped ->getForm(); 
+6
source share
2 answers

Well, I found a solution on my own after several experiments.

I will leave this question unanswered for several days, since you can post the best solution , which is really very welcome :)

In particular, I found the redundat parts of the new FormError , I donโ€™t know if there is a better way to add an error to the form. And to be honest, I donโ€™t know why new Form\CallbackValidator works, but new CallbackValidator will not.

So, be sure to add the following use statements:

 use Symfony\Component\Form as Form, // Mendatory Symfony\Component\Form\FormInterface, Symfony\Component\Validator\Constraints\MinLength, Symfony\Component\Validator\Constraints\MinLengthValidator; 

And the callback:

 $validation = function(FormInterface $form) { // If $data is null then the field was blank, do nothing more if(is_null($data = $form->getData())) return; // Create a new MinLengthValidator $validator = new MinLengthValidator(); // If $data is invalid against the MinLength constraint add the error if(!$validator->isValid($data, new MinLength(array('limit' => 3)))) : $template = $validator->getMessageTemplate(); // Default error msg $parameters = $validator->getMessageParameters(); // Default parameters // Add the error to the form (to the field "password") $form->addError(new Form\FormError($template, $parameters)); endif; }; 

Well, and this is the part that I cannot understand (why I am forced to prefix with Form ), but this is normal:

 $builder->get('password')->addValidator(new Form\CallbackValidator($validation)); 
+9
source

addValidator been deprecated and has been completely removed from Symfony 2.3.

You can do this by listening to the POST_SUBMIT event

 $builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) { $data = $event->getData(); $form = $event->getForm(); if (null === $data) { return; } if ("Your logic here") { $form->get('new_password')->addError(new FormError()); } }); 
+5
source

Source: https://habr.com/ru/post/911464/


All Articles