Error message for filling out a required date is currently like this:

  • Day field is required.
  • Month field is required.
  • Year field is required.
  • Field MyDate has errors. The year must be a number between 1 and 4000. The month must be a number between 1 and 12. The day must be a number between 1 and 31.

These are 4 lines for what is actually just 1 field. It get's even worse if you have multiple required date fields (4 error messages for each!).

Is it possible to alter this message & convert it to 1? For example: 'MyDate is required. Please fill in the correct date'.

Comments

rp7’s picture

Since nobody hasn't noticed this yet/doesn't know a solution, here's a quick fix I'm currently using to get rid of these spammy error messages. It's dirty, but it works.

In a custom module, create a new validate function for your form:

function mymodule_form_alter(&$form, $form_state, $form_id) {
     if($form_id = 'my_content_type_node_form') {
          $form['#validate'] = array('my_custom_validation');
     }
}

Then, in your new custom validation function:

function my_custom_validation() {
     
     // Retrieve all current form messages
     $errors = form_get_errors();

     // Unset these for each specific date field you might have
     unset($errors['field_mydate][0][value][day']);
     unset($errors['field_mydate][0][value][month']);
     unset($errors['field_mydate][0][value][year']);

     // Clear all current messages
     drupal_get_messages('error');
     
     // Loop through each error and add them back to the Drupal form error system
     // Notice that, in order to get the field names right, some additional processing is done (str_replace)
     foreach($errors as $key => $error) {
          form_set_error(str_replace(str_replace('][0][value', '[0][value]', $key), $error);
     }

}

Hope some more experienced developers could come up with a cleaner way to solve this though. This solution removes the 3 required messages, but keeps the generic message: "Field MyDate has errors. The year must be a number between 1 and 4000. The day must be a number between 1 and 31."

j0e’s picture

thanks for posting...i just used it in a custom module for a registration form which has content profile cck fields...and it worked to get rid of the three fields, though it suppressed some other errors like username and email address along with them, even with the (str_replace)...

also, i believe there's a typo at the end of the validate function in the above code, an extra (str_replace, which needs to be edited out for it to work...
form_set_error(str_replace(str_replace('][0][value', '[0][value]', $key), $error);
seems it should be:
form_set_error(str_replace('][0][value', '[0][value]', $key), $error);
i couldn't use it until i made this edit,

thanks again,
Joseph

ticici’s picture

thanks!
also used your quick fix, changed a bit.. (cause i had some other error messages that were lost this way..)

fonant’s picture

Component: Date CCK Field » Code

Would be nice to fix this in the date module, so it doesn't have to be fixed in every Form API form that uses a date field.

I get three "field is required" errors, and then the top-level date field error message, which is the only one that's really needed.

ExTexan’s picture

So, is the maintainer of the date module reading these posts (and another one that links to this issue)? Why don't you fix the date module?

DamienMcKenna’s picture

Issue summary: View changes
Status: Active » Closed (outdated)

We're sorry but the D6 release of Date module is no longer being supported. You are encouraged to update to Drupal 7 or 8, or direct questions to Drupal Answers.