Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are using 2.1 you may want to have a look at <a href="http://symfony.com/doc/current/book/forms.html#groups-based-on-submitted-data">"Groups based on submitted data"</a>.</p> <p><strong>Update</strong></p> <p>Example using the demo contact page <code>/demo/contact</code> in the default <code>AcmeDemoBundle</code> provided with Symfony Standard Edition :</p> <p>The form type with conditional validation groups :</p> <p> <pre><code>namespace Acme\DemoBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Validator\Constraints as Assert; class ContactType extends AbstractType { // Inject the validator so we can use it in the closure /** * @var Validator */ private $validator; /** * @param Validator $validator */ public function __construct(Validator $validator) { $this-&gt;validator = $validator; } public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('email', 'email'); $builder-&gt;add('message', 'textarea', array( // Added a constraint that will be applied if an email is provided 'constraints' =&gt; new Assert\NotBlank(array('groups' =&gt; array('EmailProvided'))), )); } public function setDefaultOptions(OptionsResolverInterface $resolver) { // This is needed as the closure doesn't have access to $this $validator = $this-&gt;validator; $resolver-&gt;setDefaults(array( 'validation_groups' =&gt; function(FormInterface $form) use ($validator) { // Get submitted data $data = $form-&gt;getData(); $email = $data['email']; // If email field is filled it will not be blank // Then we add a validation group so we can also check message field if (count($validator-&gt;validateValue($email, new Assert\NotBlank())) == 0) { return array('EmailProvided'); } }, )); } public function getName() { return 'contact'; } } </code></pre> <p>Don't forget to inject the <code>validator</code> service in the form type :</p> <pre><code>&lt;?php namespace Acme\DemoBundle\Controller; //... class DemoController extends Controller { // ... public function contactAction() { $form = $this-&gt;get('form.factory')-&gt;create(new ContactType($this-&gt;get('validator'))); // ... } } </code></pre> <p>As you can see validation of the <code>message</code> field will be triggered only if the <code>email</code> field is filled.</p> <p>Use a diff tool to catch the differences.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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