0

I'm currently building a form where the goal is that the administrator will have to choose between 3 radio buttons. If one click on any of the radios, a specific field shows up (There are 3 fields set as "invisible"). My issue is that, I have no problem making these fields show up, but they don't disappear if I select something else.

Here is my code:

function walk_handler_form_alter (&$form, FormStateInterface &$form_state, $form_id)
{
  $form['field_prestation_walk']['#states'] = [
  'visible' => [
    ':input[name="field_prestation_type"][value="walk"]' => [
        'checked' => true
      ]
    ]
  ];
}

So, as I said earlier, this code makes my forms show when I click on their related radios. However, if I click on another radio, they don't disappear.

I had a lot of troubles to find some documentation about that (well, I found some, but the examples given wheren't thorough enough for me). Also, I tried to add an invisible after the visible one, with the exact condition except that 'checked' was set to false, but it didn't work.

Should I use plain Javascript / jQuery for this, or am I just it the wrong way?

Thanks in advance

3

Hello try this ...

$form['field_prestation_walk']['#states'] = [
  'visible' => [
    ':input[name="field_prestation_type"]' => [
        'value' => 'walk'
      ],
    ]
  ];
  • Thank you, it was easier than expected! – Jaeger Dec 9 '19 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.