0

I have a form with 3 radio fields and a textfield in it.

Radio Field 1,Radio Field 2,Radio Field 3 and Textfield

I'm using #states to make: Radio Field 2 only visible if certain input from Radio Field 1 is done. Radio Field 3 only visible if certain input from Radio Field 2 is done. Textfield is only visible if certain input from Radio Field 3 is done.

So the conditions are a little nested.

The problem: The visibility condition for the Textfield is not working. After playing around with it it seems like, that i nested "too deep" since changing the condition for the Textfield to e.g. Radio Field 2 or 1 works without a problem.

Is there a way around this?

I am using drupal 7.

Here is some code for better understanding:

    class FormFields {
        public function RadioField1() {
        return [
          '#type' => 'radios',
          '#title' => t('Radio Field 1'),
          '#options' => OPTIONS1,
        ];
      }

        public function RadioField2() {
            return [
              '#type' => 'radios',
              '#title' => t('Radio Field 2'),
              '#options' => OPTIONS2,
              '#states' => [
                'visible' => [
                  ':input[name*="radio_field_one"]' => [
                    ['value' => 'option1'],
                    ['value' => 'option2'],
                  ],
                ],
              ],
            ];
          }

        public function RadioField3() {
            return [
              '#type' => 'radios',
              '#title' => t('Radio Field 3'),
              '#options' => OPTIONS3,
              '#states' => [
                'visible' => [
                  ':input[name*="radio_field_two"]' => [
                    ['value' => 'option1'],
                    ['value' => 'option2'],
                  ],
                ],
              ],
            ];
          }

        //THIS TEXTFIELD DOES NOT SHOW UP IF OPTION 3 IS CHOOSEN!
        public function Textfield() {
            return [
              '#type' => 'textfield',
              '#title' => 'Textield',
              '#states' => [
                'visible' => [
                  ':input[name*="radio_field_three"]' => [
                    'value' =>
                      'option3',
                  ],
                ],
              ],
            ];
          }
}



 function Form($form, &$form_state) {
    $forms = new FormFields();
    $form['fieldset-generals']['radio_field_one'] = $forms->RadioField1();
    $form['fieldset-generals']['radio_field_two'] = $forms->RadioField2();
    $form['fieldset-generals']['radio_field_three'] = $forms->RadioField3();
    $form['fieldset-generals']['textfield'] = $forms->Textfield();
    //Other stuff
    }

Your Answer

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

Browse other questions tagged or ask your own question.