Symfony2 Sonata admin dynamically change input data based on selected value

前端 未结 3 2015
借酒劲吻你
借酒劲吻你 2021-01-03 17:11

For one of my objects I need to create some dynamic form rendering... But I cant figure out how to do this in Sonata Admin. For example when I create an object I have a fiel

3条回答
  •  心在旅途
    2021-01-03 17:39

    Sonata provides you with the 'sonata_type_choice_field_mask' type which allows you to change the fields displayed on the form dynamically depending on the value of this 'sonata_type_choice_field_mask' input so you don't have to use ajax.

    Here is the doc where you can find everything about sonata types and the choice field mask.

    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('type', 'sonata_type_choice_field_mask', array(
                'choices' => array(
                    //The list of available 'Type' here
                    'choice1',
                    'choice2'
                ),
                'map' => array(
                    //What you want to display depending of the selected option
                    'choice1' => array(
                        // List of the fields displayed if choice 1 is selected
                        'field1', 'field3'
                    ),
                    'choice2' => array(
                        // List of the fields displayed if choice 2 is selected
                        'field2', 'field3'
                    )
                ),
                'placeholder' => 'Choose an option',
                'required' => true
            ))
            ->add('field1', 'entity', array(/* Options for entity1 goes here */))
            ->add('field2', 'entity', array(/* Options for entity2 goes here */))
            ->add('field3')
        ;
    }
    

提交回复
热议问题