0

I have a form with two radios and two forms related to each of them. These forms have some required fields. user can just check one radio and then fill in one form. but then clicks on submit button drupal gives error for those unselected and empty field ( which are required but now they are not required because their radio isn't selected) .

How can I bypass those required inputs in my module ? I am developing my module's form with Form API.

2 Answers 2

1

If you are building your own form, you need to remove '#required' => TRUE, from field to bypass validation or assign FALSE value.

Example with required field:

$form['title'] = array(
  '#type' => 'textfield', 
  '#title' => t('Subject'), 
  '#default_value' => 'whatever', 
  '#size' => 60, 
  '#required' => TRUE, // remove this if you want to bypass or give it FALSE value
);

Check Form API Reference for more details

Now you can do the same for existing form by alter the form using hook_alter_form and unset required attribute or assign FALSE value.

Example:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'FORM_ID') {
    $form['FIELD_NAME']['#required'] = FALSE;
    // or
    unset($form['FIELD_NAME']['#required']);
  }
}
0

You can use #limit_validation_errors in Drupal 7. See https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#limit_validation_errors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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