Hi all

WebForm is working is very nicely, thanks, but is there a way that I can change the message that appears if a required field is not entered?

I have "Disclaimer" field which is a select type with one ("multiple") value so it appears as a checkbox. I'd like to change the message so it says something like "You must check the Disclaimer box" rather than "Disclaimer field is required".

Thanks
Ross

Comments

I think you can do this with http://drupal.org/project/webform_validation, but I'm not sure. All the required field messages are created directly by Drupal.

Perhaps you could try a variable dump of $_SESSION['messages'][$type] in Additional Validation and see if you can work out a way to replace the default field required message with something else?

@quicksketch

Thanks but unfortunately webform_validation doesn't do customized messages.

I found this http://drupal.org/node/742344 discussion about enhancing the form api in Drupal 7 to allow customized messages. That would certainly be useful and I think it would only require a simple change to WebForm to make use of it.

If I get desperate I guess I can hack $messages in the page template. Unfortunately it's already a rendered list at that point. I'm not sure if it's available earlier as an array.

@vernond

Thanks. I got that to work. It's a horrible ugly hardcoded hack of course but ... it will do for now.

I put this in my theme's template.php. I'm using a Zen subtheme.

function mytheme_preprocess_node_webform(&$vars, $hook) {
  if ($vars['node']->nid == my_node_id && is_array($_SESSION['messages']['error'])) {
    $errors = &$_SESSION['messages']['error'];
    $find = 'Disclaimer field';
    for($idx = 0; $idx < count($errors); $idx++) {
      if (substr($errors[$idx], 0, strlen($find)) == $find) {
        $errors[$idx] = 'You must check the disclaimer confirmation box.';
        break;
      }
    }
  }
}

Cool. As you say, it's a bit of a wrench, but it can't be knocked too badly if it works I guess.

I got solution. But it is bit of hard code in core system.
In my case, I need custom error message for CAPTCHA, when it is required but did not input value, I want simply show "CAPTCHA is reqired" . what I changed is write script in function _form_validate at /drupal/includes/ form.inc
The previous code is like:

function _form_validate($elements, &$form_state, $form_id = NULL) {
....
  // Validate the current input.
  if (!isset($elements['#validated']) || !$elements['#validated']) {
....
    if (isset($elements['#needs_validation'])) {
          if ($elements['#required'] && (!count($elements['#value']) || (is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0))) {
              form_error($elements, $t('!name field is required.', array('!name' => $elements['#title'])));
             }
          }
....
}

After Change,
function _form_validate($elements, &$form_state, $form_id = NULL) {
....
  // Validate the current input.
  if (!isset($elements['#validated']) || !$elements['#validated']) {
....
    if (isset($elements['#needs_validation'])) {
          if ($elements['#required'] && (!count($elements['#value']) || (is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0))) {
  // Add special validation for the field your expected, and custom error messages for particular fields.
              if ($elements['#id'] == 'YourFieldID') {
                       form_error($elements, $t('TYPE MESSAGE YOU WANT.', array('!title' => $elements['#title'])));
              }
        else {
        form_error($elements, $t('!name field is required.', array('!name' => $elements['#title'])));
        }
      }
   }
...
}

This is a global change, whenever this particular required field required in varies forms, this customised message will show. :)

I think you can do this with http://drupal.org/project/webform_validation, but I'm not sure. All the required field messages are created directly by Drupal.

I've filled a bug report for webform validation for address this problem #1110506: make regular expression validation more flexible

a simple .+ regular expression could be a simple substitution for required, and is possible to add a custom error

Closed #1148508: Can I add error message to webform? as duplicate. As others have stated this is technically a difficult thing to do within Drupal core's constraints, since #required has an automatic behavior and is hard-coded into Drupal core. However Webform does have an advantage over most other forms in Drupal in that it already re-implements the core validation system in order to handle multiple pages correct (otherwise you couldn't go "Back" without triggering validation errors). In the _webform_client_form_validate() function we've actually copied all the code out of _form_validate() that contains the code for handing #required messages, most notably:

<?php
     
if ($elements['#required'] && (!count($elements['#value']) || (is_string($elements['#value']) && strlen(trim($elements['#value'])) == 0))) {
       
form_error($elements, t('!name field is required.', array('!name' => $elements['#title'])));
      }
?>

So we do have the option of introducing our own error handling here if we can figure out a way to fit it into either UI or the API. I personally think a new property like '#required_message' might be a good way to go (and it looks like this is the approach recommended in #742344: Allow forms to set custom validation error messages on required fields too, nice).

Fitting this into the UI might be a bit trickier though. At least implemented at the API level would be a start and open the door for Webform Validation to include better handling if desired.

While the additional functionality comes available here this is a quick a dirty way of adding a customised message to the default message. Note the method will apply to all your webform massages but you can find or add some unique class' or IDs on the relavent page to make the unique messages.

.node-type-webform .error:before{
display:block;
margin-bottom:10px;
font-weight:600;
content: "ADD YOUR MESSAGE HERE.";
}

@chaloum: Note that definitely won't work in IE6 or IE7. See http://www.quirksmode.org/css/contents.html

It'll also likely conflict with accessibility tools like screenreaders, but I don't know that for sure.

Title:How can I change the message for a required field?Add capability to use custom error message for required field
Version:6.x-3.1» 7.x-3.x-dev
Component:User interface» Miscellaneous
Category:support» feature
Status:Active» Needs review
StatusFileSize
new11.11 KB

I personally think a new property like '#required_message' might be a good way to go (and it looks like this is the approach recommended in #742344: Allow forms to set custom validation error messages on required fields too, nice).
Fitting this into the UI might be a bit trickier though. At least implemented at the API level would be a start and open the door for Webform Validation to include better handling if desired.

I have tried to make a patch also for UI, but I've used #required_error instead of #required_message for consistence with the #742344: Allow forms to set custom validation error messages on required fields patch.

I have tried to make a patch also for UI, but I've used #required_error instead of #required_message for consistence with the #742344: Allow forms to set custom validation error messages on required fields patch.

obviously, if needed, I could change patch for use #required_message and/or split patch in some little patches for make review more simple.

No worries robertom, this looks great! A very nice patch. I'm struggling slightly with the UI issue still, this is definitely something that I look at and stop for a second to try to figure out what it does, probably because I've never actually seen a form builder (Wufoo, SurveyMonkey, others) include this option. Of course everyone in this issue will be heavily biased towards adding it no matter what, but I'm trying to figure out if there's anyway we can make this option fit better into the UI.

The patch from #12 didn't apply cleanly anymore on the 7.x-3.9-hotfix branch (is that currently the right one to prepare patches for?), so I made a new one (I hope I didn't skip any necessary changes).

Should have started from the master branch of course, will make another patch.

New patch for master.

I encountered an additional issue when using a component with required radio buttons. When submitting without selection an option, an error "An illegal choice has been detected" was raised. I fixed this also in the patch, in webform.module @@ -2088,7 +2096,7. Seems to be related to a couple of other issues mentioned in the issue queue here, which were closed though.

Related request. Would it be possible to customize the general error message (form already submitted, you're not allowed to view this form, etc). theme_webform_view_messages

I guess one could conceivably customize this per node, but I'm more interested in changing the default strings across the board.

@Icosa I tried the same code but it did not work. In place of my_node_id I put 41 (the webform is 41th node). I gave $find="name". Is there anything else which I need to change in my code ?

Version:7.x-3.x-dev» 7.x-4.x-dev
Status:Needs review» Needs work

New features are only being added to the 4.x branch at this point.

There is a similar issue for unique validation: #1089132: Custom error for unique property

Some scenarios are still using #webform_component instead of the dedicated #required_error:

-      form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
+      $component = $element['#webform_component'];
+      if (isset($component['extra']['required_error']) && drupal_strlen($component['extra']['required_error'])) {
+        form_error($element, $component['extra']['required_error']);
+      }
+      else {
+        form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
+      }

Still not sure how I feel about incorporating this into the UI.

So my suggestions to make this fit the UI better:

- Don't show the text field for customizing the required message unless the checkbox is checked to make it required in the first place. (similar to #1667662: Hide Text for "Other..." textfield by default for Select components)
- Populate the textfield with the default error message at all times, but compare this value against the default on save. Only save the string if it has been customized.
- For the most flexibility, the default string should be a variable retrieved through webform_variable_get().

If this patch is updated, I'd like to see #1089132: Custom error for unique property happen at the same time so we have consistency in providing custom validation messages.