Sonata form field - change field based on selection of another model field

I have a very simple problem and I think this is very common too. My sonata admin configureFormFields function looks like this:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('status', 'sonata_type_model', array(
            'required'  => true,
            'btn_add'   => false,
            'expanded'  => true,
        )
        ->add('activeReason', 'hidden')
        ->add('inactiveReason', 'hidden')
        ->add('onHoldReason', 'hidden')
    ;
}

      

The 'status' object has a __toString () method and values:

  • Active
  • Inactive
  • While holding down the key

I want that if I select "Active" from state, the form field "activeReason" should be shown, whereas selecting "Inactive" will hide the other field of reason 2 and just show the field "inactiveReason" and so with the option Hold (it will hide the other 2 and will just display the form field onHoldReason).

I know this is possible with jQuery custom functions, but is there a SONATA ADMIN WAY or SYMFONY WAY way? I am fully aware of the type of sonata form sonata_type_choice_field_mask

, which should have many variations. Is there a way to combine this functionality, or do something similar from this type?

+3


source to share


1 answer


You can do this with this type of sonata. Take a look at this link, it had the same problem and solved it:



SonataAdmin - sonata_type_choice_field_mask

+1


source







All Articles