Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
attiks CreditAttribution: attiks commentedClientside validation checks for '#required' on the item level, you could try the following
Comment #2
Anonymous (not verified) CreditAttribution: Anonymous commentedYeah that makes it conditional on the frontend, but always required on server-side.
Comment #3
attiks CreditAttribution: attiks commentedTo be sure, you enabled 'Clientside Validation States'?
Comment #4
Jelle_SComment #5
haggins CreditAttribution: haggins commentedSame 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?
Comment #6
leducvin CreditAttribution: leducvin commentedStill a problem today in 7.x-2.x-dev. I am using hook_form_alter to set a conditional required state on a field:
100 % of the rest of the form is validated client-side correctly. However when
field_some_select_list
has the value 3, a red asterisk shows up on form to indicate thefield_some_text_area
is required, as expected, but client-side validation doesn't happen.Because I set a validation callback:
,upon form submission, the form is validated server-side. If I don't set the callback, them the field just goes unvalidated.
I have the clientside_validation_states module enabled.