2

It's possible to skip required body field validation, if it's empty? I can add custom validation on the first place, like:

  $form_validate = $form['#validate'][0];
  $form['#validate'][0] = 'remove_body_field_validation';
  $form['#validate'][] = $form_validate;

function remove_body_field_validation($form, &$form_state) {
 $form_state['complete form']['settings']['body_field']['body']['#required'] = FALSE;
}

But i think the "required validation" runs before "remove_body_field_validation"?? :-/

6

Why don't you simply go to Structure -> Content Types -> [Your type] -> Manage Fields -> Body -> Edit uncheck "Required field"? Since Drupal 7, body behaves as any other field...

If you really need to do this in code, then you do it in form_alter like this:

function [mymodule]_form_alter(&$form, &$form_state, $form_id) {
  // some logic here to filter which form is affected (or use hook_form_FORM_ID_alter)
  $form['body'][LANGUAGE_NONE][0]['#required'] = FALSE;
}

Is there any particular reason for you to do it during validation and not on form render? You stated that you need to "skip required body field validation, if it's empty" which is equivalent to turning the "required" completely.

EDIT: I found a way to do this (based on this - https://api.drupal.org/comment/28464#comment-28464):

function my_validate($form, &$form_state) {
  $errors = form_get_errors();

  if ($errors) {
    // Clear errors.
    form_clear_error();
    // Clear error messages.
    $error_messages = drupal_get_messages('error');
    // Initialize an array where removed error messages are stored.
    $removed_messages = array();


    // Remove all errors originated by the 'foo][bar' element.
    foreach ($errors as $name => $error_message) {
      if ($name == 'body][und][0][value') {
        $removed_messages[] = $error_message;
        unset($errors[$name]);
      }
    }


    // Reinstate remaining errors.
    foreach ($errors as $name => $error) {
      form_set_error($name, $error);
      // form_set_error() calls drupal_set_message(), so we have to filter out
      // these from the error messages as well.
      $removed_messages[] = $error;
    }


    // Reinstate remaining error messages (which, at this point, are messages that
    // were originated outside of the validation process).
    foreach (array_diff($error_messages['error'], $removed_messages) as $message) {
      drupal_set_message($message, 'error');
    }
  }
}
  • I need to skip "required validation" after submission :) I can disable "required option" in menu and remove this option via code $form['body'][LANGUAGE_NONE][0]['#required'] = FALSE; before submit, but i need it after submission. – QQQ Dec 31 '13 at 7:40
  • I see. Can you explain why you need it like that and not on form render? Because it seems that it is not possible, and maybe there is another solution to your problem. – m1n0 Dec 31 '13 at 9:18
  • I found the solution - check the edited answer. – m1n0 Dec 31 '13 at 9:37
  • Of course this complete answer deserves a point! Thank you! – Ehsan Feb 10 '15 at 15:22
0

To clear previous validation errors, you can clear them in form_set_error static variable, e.g. add callback to #element_validate to your element and try the following function:

function remove_body_field_validation(&$element, &$form_state, $form) {

  if ($element['#field_name'] == 'body') {
    $element['#required'] = FALSE;
    // Clear previous validation errors.
    $errors = &drupal_static('form_set_error', array());
    $err_key = $element['#field_name'] . '][' . LANGUAGE_NONE;
    if (array_key_exists($err_key, $errors)) {
      unset($errors[$err_key]);
    }
    return;
  }

}

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.