1

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.

Plems
  • 11
  • 1
  • 4
  • Have you tried using an event listener, like [here](http://stackoverflow.com/questions/26246192/correct-way-to-use-formevents-to-customise-fields-in-sonataadmin) ? – greg0ire Jun 08 '16 at 21:55
  • I think that's exactly what I'm looking for, thanks greg0ire. – Plems Jun 09 '16 at 07:36
  • Would you consider making a PR to document this in a cookbook article ? – greg0ire Jun 09 '16 at 09:37
  • It's finally not working for me. If I'm not wrong, there is no event that fires when a field value change. All I managed to do is this : If I select event and then save, when I edit the object the date field become available. But it doesnt switch between available or not availabe when I change of item in the list. – Plems Jun 09 '16 at 11:57
  • Oh you mean client-side ? Indeed there is nothing, but I think some people talked about it recently, let me look for the link. – greg0ire Jun 09 '16 at 13:50
  • Here : https://github.com/sonata-project/SonataAdminBundle/issues/3808 – greg0ire Jun 09 '16 at 13:54

2 Answers2

4

Maybe: https://sonata-project.org/bundles/admin/master/doc/reference/form_types.html#sonata-adminbundle-form-type-choicefieldmasktype if you just show additional field based on choice

Artyum
  • 103
  • 2
  • 8
Armstrong
  • 41
  • 1
2

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')
        ;
    }
}
jclyons52
  • 306
  • 3
  • 8