I have a date field on a drupal node edit form using the pop-up calendar widget. When certain things happen on my site I have to disable some fields on that node edit form, including the date.

However, when this happens, I can no longer save the form because I get this message:

The value input for field [my field] is invalid.

I tried changing the widget to plain text, and it works fine, so it is an issue with the pop-up calendar widget.

Any ideas?

  • How are you disabling the form element? – David Thomas Jun 12 '14 at 8:36
  • In a form_alter hook: $form[$field_id]['#disabled'] = true; – Cameron Ball Jun 12 '14 at 8:42

If using hook_form_alter you can use #limit_validation_errors to exclude that field from validation.

The #limit_validation_errors property is a list of "sections" within $form_state['values'] that must contain valid values. Each "section" is an array with the ordered set of keys needed to reach that part of $form_state['values'] (i.e., the #parents property of the element).

Example 1: Allow the "Previous" button to function, regardless of whether any user input is valid.

  $form['actions']['previous'] = array(
    '#type' => 'submit',
    '#value' => t('Previous'),
    '#limit_validation_errors' => array(),       // No validation.
    '#submit' => array('some_submit_function'),  // #submit required.

Example 2: Require some, but not all, user input to be valid to process the submission of a "Previous" button.

  $form['actions']['previous'] = array(
    '#type' => 'submit',
    '#value' => t('Previous'),
    '#limit_validation_errors' => array(
      array('step1'),       // Validate $form_state['values']['step1'].
      array('foo', 'bar'),  // Validate $form_state['values']['foo']['bar'].
    '#submit' => array('some_submit_function'), // #submit required.

More info: form_set_error

| improve this answer | |

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.