Symfony2: how to add form constraint for a field in bind PRE_SET_DATA depending on data

I have a form in Symfony 2 with basically two fields:

public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('contactType', 'select', array( 'choices' => $contactTypes )) ->add('value', 'text'); } 

Then I added an EventSubscriber that listens for the FormEvents :: PRE_SET_DATA event. What I really want to do is change the method of verification depending on the value of contactType (numerical values ​​from 1 to 4, which indicate email, mobile, fixed line and fax).

I followed this tutorial http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html

but I cannot figure out how to add a constraint to the value field.

Can anyone help me? Thank you very much in advance.

+4
source share
2 answers

Instead of adding dynamic constraints dynamically in the event event subscriber (not sure if this is even possible), you can set groups to field validation constraints and define verification groups based on the presented data .

+8
source

Function for creating a form from the controller:

 <?php // ... class DefaultController extends Controller { /** * * @param \Clicproxy\DeltadocCabBundle\Entity\Mark $mark * @param \Clicproxy\DeltadocCabBundle\Entity\Tag $tag * @return Form */ private function createTagForm(Mark $mark, Tag $tag) { $form = $this->createForm(new TagType(), $tag, array( 'action' => $this->generateUrl('tag_new', array('slug' => $this->slugify($mark->getName()))), 'method' => 'POST', )); foreach ($mark->getFields() as $field) { $form->add($this->slugify($field->getName()), $field->getFormType(), $field->getOptions()); } $form->add('submit', 'submit', array('label' => 'crud.default.save')); return $form; } // ... 

Code in the object (type, restrictions, ...):

 <?php // ... /** * Field * * @ORM\Table() * @ORM\Entity * @UniqueEntity({"name", "mark"}) */ class Field { // ... /** * * @return array */ public function getOptions() { $options = array('label' => $this->getName(), 'mapped' => FALSE); $options['required'] = $this->getType() != 'checkbox'; if ('date' == $this->getType()) { $options['attr']['class'] = 'datepicker'; // 'input-group date datepicker'; $options['attr']['data-date-format'] = 'dd/mm/yyyy'; $options['attr']['data-date-autoclose'] = true; } if ('choice' == $this->getType()) { $choices = array(); foreach ($this->getChoices() as $choice) { $choices[$choice->getValue()] = $choice->getName(); } asort($choices); $options['choices'] = $choices; } $options['constraints'] = $this->getValidationConstraint(); return $options; } public function getValidationConstraint () { $validation_constraint = array(); if ('number' == $this->getType()) { if (0 < $this->getMaximum()) { $validation_constraint[] = new LessThanOrEqual (array( 'message' => 'entity.field.number.lessthanorequal', // {{ compared_value }} 'value' => $this->getMaximum() )); } if (0 < $this->getMinimum()) { $validation_constraint[] = new GreaterThanOrEqual(array( 'message' => 'entity.field.number.greaterthanorequal', // {{ compared_value }} 'value' => $this->getMinimum() )); } } elseif ('text' == $this->getType ()) { if (0 < $this->getMaximum()) { $validation_constraint[] = new Length(array( 'min' => $this->getMinimum() > 0 ? $this->getMinimum() : 0, 'max' => $this->getMaximum() > 0 ? $this->getMaximum() : 0, 'minMessage' => 'entity.field.text.minMessage', // {{ limit }} 'maxMessage' => 'entity.field.text.maxMessage', 'exactMessage' => 'entity.field.text.exactMessage', )); } } elseif ('date' == $this->getType()) { } return $validation_constraint; } // ... 

All this code actually works.

At the same time, you have a solution for creating forms on the fly with restrictions.

0
source

All Articles