Symfony2: How to use constraints on custom compound form type? Symfony2: How to use constraints on custom compound form type? symfony symfony

Symfony2: How to use constraints on custom compound form type?


I suggest you read again the documentation about validation first.

What we can make out of this is that validation primarily occurs on classes rather than form types. That is what you overlooked. What you need to do is:

  • To create a data class for your TextWithAutoValueType, called src/My/Bundle/Form/Model/TextWithAutoValue for instance. It must contain properties called text and checkbox and their setters/getters;
  • To associate this data class to your form type. For this, you must create a TextWithAutoValueType::getDefaultOptions() method and populate the data_class option. Go here for more info about this method;
  • Create validation for your data class. You can either use annotations or a Resources/config/validation.yml file for this. Instead of associating your constraints to the fields of your form, you must associate them to the properties of your class:

validation.yml:

src/My/Bundle/Form/Model/TextWithAutoValue:    properties:        text:            - Type:                type: string            - NotBlank: ~        checkbox:            - Type:                type: boolean

Edit:

I assume that you already know how to use a form type in another. When defining your validation configuration, you can use a very useful something, called validation groups. Here a basic example (in a validation.yml file, since I'm not much proficient with validation annotations):

src/My/Bundle/Form/Model/TextWithAutoValue:    properties:        text:            - Type:                type: string                groups: [ Default, Create, Edit ]            - NotBlank:                groups: [ Edit ]        checkbox:            - Type:                type: boolean

There is a groups parameter that can be added to every constraint. It is an array containing validation group names. When requesting a validation on an object, you can specify with which set of groups you want to validate. The system will then look in the validation file what constraints should be applied.

By default, the "Default" group is set on all constraints. This also is the group that is used when performing a regular validation.

  • You can specify the default groups of a specific form type in MyFormType::getDefaultOptions(), by setting the validation_groups parameter (an array of strings - names of validation groups),
  • When appending a form type to another, in MyFormType::buildForm(), you can use specific validation groups.

This, of course, is the standard behaviour for all form type options. An example:

$formBuilder->add('auto_text', 'textWithAutoValue', array(    'label' => 'my_label',    'validation_groups' => array('Default', 'Edit'),));

As for the use of different entities, you can pile up your data classes following the same architecture than your piled-up forms. In the example above, a form type using textWithAutoValueType will have to have a data_class that has a 'auto_text' property and the corresponding getter/setter.

In the validation file, the Valid constraints will be able to cascade validation. A property with Valid will detect the class of the property and will try to find a corresponding validation configuration for this class, and apply it with the same validation groups:

src/My/Bundle/Form/Model/ContainerDataClass:    properties:        auto_text:            Valid: ~ # Will call the validation conf just below with the same groupssrc/My/Bundle/Form/Model/TextWithAutoValue:    properties:        ... etc


As described here https://speakerdeck.com/bschussek/3-steps-to-symfony2-form-mastery#39 (slide 39) by Bernhard Schussek (the main contributor of symofny form extension), a transformer should never change the information, but only change its representation.

Adding the information (checkbox' => true), you are doing something wrong.

In Edit:

public function buildForm(FormBuilderInterface $builder, array $options){    $builder->add('value', 'text', $options);    $builder->add('checkbox', 'checkbox', array('mapped'=>false));    $builder->addModelTransformer(        new TextWithAutoValueTransformer()    );}