I have a form field that is conditionally required depending on another field's value. Using Form API:

          $form['details_' . $method['provider'] . '_' . $code]['lastname'] = array(
            '#type' => 'textfield',
            '#title' => t('Last Name'),
            '#disabled' => $disabled,
            '#states' => array(
              'visible' => array(':input[name="method"]' => array('value' => $code)),
              'required' => array(':input[name="method"]' => array('value' => $code))
            ),
          );

The field shows up as required, but somehow fails to respect clientside validation?

Comments

attiks’s picture

Clientside validation checks for '#required' on the item level, you could try the following

          $form['details_' . $method['provider'] . '_' . $code]['lastname'] = array(
            '#type' => 'textfield',
            '#title' => t('Last Name'),
            '#disabled' => $disabled,
            '#required' => TRUE,
            '#states' => array(
              'visible' => array(':input[name="method"]' => array('value' => $code)),
              'required' => array(':input[name="method"]' => array('value' => $code))
            ),
          );
Mathijs Koenraadt’s picture

Yeah that makes it conditional on the frontend, but always required on server-side.

attiks’s picture

To be sure, you enabled 'Clientside Validation States'?

Jelle_S’s picture

Status: Active » Postponed (maintainer needs more info)
haggins’s picture

Status: Postponed (maintainer needs more info) » Active

Same here. There's no clientside validation error but one from serverside after submitting the form. "Clientside Validation States" is enabled. The field is set #required and should only be required if the conditional requirements were hit.

Is a behavior like that the intention behind this submodule or does it affect only #rules validation by design?