If I have a multiple field (ajax) required: FIELD_A (N), and I want it not required if: FIELD_B has a certain value. How can I do ? Any function in $ form_state?


Use states to style the input as required. This won't actually perform any validation but will add the required asterisk to the field when field B has a certain value.

$form['FIELD_A']['#states'] = [
  'required' => array(
    ':input[name="FIELD_B"]' => array('value' => 'foo'),

Then use custom validation to actually check FIELD_A has the correct value depending on the value of FIELD_B.

| improve this answer | |
  • ! Ok thz. It did not work due to a bug on a contrib field, then ok. – BOES Nov 28 '17 at 12:23

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.