Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 form validator groups without entities
    primarykey
    data
    text
    <p>I'm using Symfony2 form component to build and validate forms. Now I need to setup validator groups based on a single field value, and unfortunately it seems that every example out there is based on entities - which im not using for several reasons.</p> <p>Example: If task is empty, all constraint validators should be removed, but if not, it should use the default set of validators (or a validator group). </p> <p>In other words, what I'm trying to achieve is making subforms optional, but still be validated if a key field is populated.</p> <p>Can someone possible give me an example how to configure it?</p> <pre><code>&lt;?php namespace CoreBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Validator\Constraints as Assert; use CoreBundle\Form\Type\ItemGroupOption; class ItemGroup extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('title', 'text', array( 'label' =&gt; 'Titel', 'attr' =&gt; array('class' =&gt; 'span10 option_rename'), 'required' =&gt; false )); $builder-&gt;add('max_selections', 'integer', array( 'label' =&gt; 'Max tilvalg', 'constraints' =&gt; array(new Assert\Type('int', array('groups' =&gt; array('TitleProvided')))), 'attr' =&gt; array('data-default' =&gt; 0) )); $builder-&gt;add('allow_multiple', 'choice', array( 'label' =&gt; 'Tillad flere valg', 'constraints' =&gt; array(new Assert\Choice(array(0,1))), 'choices' =&gt; array(0 =&gt; 'Nej', 1 =&gt; 'Ja') )); $builder-&gt;add('enable_price', 'choice', array( 'label' =&gt; 'Benyt pris', 'constraints' =&gt; array(new Assert\Choice(array(0,1))), 'choices' =&gt; array(0 =&gt; 'Nej', 1 =&gt; 'Ja'), 'attr' =&gt; array('class' =&gt; 'option_price') )); $builder-&gt;add('required', 'choice', array( 'label' =&gt; 'Valg påkrævet', 'constraints' =&gt; array(new Assert\Choice(array(0,1))), 'choices' =&gt; array(0 =&gt; 'Nej', 1 =&gt; 'Ja') )); $builder-&gt;add('options', 'collection', array( 'type' =&gt; new ItemGroupOption(), 'allow_add' =&gt; true, 'allow_delete' =&gt; true, 'by_reference' =&gt; false ) ); $builder-&gt;add('sort', 'hidden'); } public function getName() { return 'item_group'; } public function setDefaultOptions(OptionsResolverInterface $resolver) { global $app; $resolver-&gt;setDefaults(array( 'validation_groups' =&gt; function(FormInterface $form) use ($app) { // Get submitted data $data = $form-&gt;getData(); if (count($app['validator']-&gt;validateValue($data['title'], new Assert\NotBlank())) == 0) { return array('TitleProvided'); } else { return false; } }, )); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload