Plems
Plems

Reputation: 11

Sonata Admin Bundle : enable field dynamically

I use Sonata Admin Bundle in my Symfony2 project. In one form, I have a list of choice containing two items, 'article' and 'event', and a date field, which is relevant only if 'event' is selected in the list.

How can I disabla/enable according to which value in the list is selected?

Here is my relevant code :

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('title', null, array(
            'label' => 'Titre',
        ))
        ->add('type', 'choice', array('choices' => array('0' => 'Article', '1' => 'Evénement',)) )
        ->add('gameDate', null, array('required' => false));
 }

Thank you for your help.

Upvotes: 1

Views: 3622

Answers (2)

jclyons52
jclyons52

Reputation: 316

just to save you some scrolling, here's the relevant section from the doc:

13.1.5. SONATA\ADMINBUNDLE\FORM\TYPE\CHOICEFIELDMASKTYPE According the choice made only associated fields are displayed. The others fields are hidden.

<?php
// src/AppBundle/Admin/AppMenuAdmin.php

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Form\Type\ChoiceFieldMaskType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class AppMenuAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('linkType', ChoiceFieldMaskType::class, [
                'choices' => [
                    'uri' => 'uri',
                    'route' => 'route',
                ],
                'map' => [
                    'route' => ['route', 'parameters'],
                    'uri' => ['uri'],
                ],
                'placeholder' => 'Choose an option',
                'required' => false
            ])
            ->add('route', TextType::class)
            ->add('uri', TextType::class)
            ->add('parameters')
        ;
    }
}

Upvotes: 2

Related Questions