26508

Add validation constraints on mapped = false fields for a specific validation group

Question:

I'd like to be able to add in a Form class additional validation constraints for a specific validation group. How could I do that ?

Since Symfony 2.1, adding a validation while building the form looks like this :

use Symfony\Component\Validator\Constraints\MinLength; use Symfony\Component\Validator\Constraints\NotBlank; $builder ->add('firstName', 'text', array( 'constraints' => new MinLength(3), )) ->add('lastName', 'text', array( 'constraints' => array( new NotBlank(), new MinLength(3), ), )) ;

<a href="http://symfony.com/blog/form-goodness-in-symfony-2-1" rel="nofollow">sources</a>

Is there a way to assign them to a validation constraint ?

In my case, I have <a href="http://symfony.com/doc/2.2/book/forms.html#groups-based-on-submitted-data" rel="nofollow">validation groups depending on submitted data</a>

Thanks in advance for your suggestions

Answer1:

OK, well the solution was pretty straighforward actually.

Looking at the <a href="http://api.symfony.com/2.2/Symfony/Component/Validator/Constraint.html" rel="nofollow">Constraint</a> class, I noticed the exposed $groups property and addImplicitGroupName(string $group) method.

When you know that, you know all about it:

$cv1 = new NotBlank(); $cv1->groups = array('myGroup'); $cv2 = new NotNull(); $cv2->groups = array('myGroup'); $myCnstrs = array( 'constraints' => array( $cv1, $cv2, ) ); $myOtherOptions = array( ... ); $builder->add('myField', null, array_merge($myCnstrs,$myOtherOptions));

Sorry if I abused by posting a question and replying to it right after...

Recommend

  • Doctrine 2 @Gedmo\\SoftDeleteable and unique fields
  • How to not return an specific column using PagingAndSortingRepository
  • Aurelia validation: applying some rule on change and some on blur on same property
  • Puzzle taken from Gardner
  • py2neo - How can I use merge_one function along with multiple attributes for my node?
  • Executing merge statement via C# SqlCommand not working
  • Enforce an Edge to connect two distinct vertex classes
  • Sql Server 2005 Xml data type has CDATA removed on INSERT
  • At what point is code reordering in C++ optimization stopped?
  • Why can't I delete a model instance with related objects?
  • Trouble understanding GHC complaint about ambiguity
  • How can I generate integers that satisfy some restrictions?
  • JqueryUI Autocomplete : only one character is displayed per list item
  • uniqness insertion in native xml database
  • Column Nullability/Optionality: NULL vs NOT NULL
  • Joining Model with Table Laravel 5.2
  • SVG Fill Width to Child Elements
  • if I want to find what's referencing an object in SQL Server, is searching syscomments comprehe
  • How to set the navigation bar to the top in Table View?
  • UIPickerView without IB?
  • Doctrine2 inverse persistance not working in nested forms
  • Cassandra NoClassDefFoundError: com/google/common/util/concurrent/AsyncFunction
  • JQuery Auto-Complete: How do I handle modifications?
  • Get current user from inside the model in Sails
  • How to Model Ternary Relationship in CakePhp?
  • Criterion causing memory consumption to explode, no CAFs in sight
  • SqlCommand back up Database
  • jquery validation - waiting for remote check to complete
  • uml Composition relationships to RDF and OWL
  • Assign variable to the value in HTML
  • Bad request using file_get_contents for PUT request in PHP
  • NHibernate Validation Localization with S#arp Architecture
  • Dynamically accessing properties of knockoutjs observable array
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Redux, normalised entities and lodash merge
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?