Reputation: 11
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
Reputation: 41
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
Upvotes: 4
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