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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

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.

vernond’s picture

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?

icosa’s picture

@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.

icosa’s picture

@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;
      }
    }
    
  }
}
vernond’s picture

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

bevin’s picture

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. :)

robertom’s picture

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

robertom’s picture

quicksketch’s picture

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:

      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.

chaloum’s picture

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.";
}
quicksketch’s picture

@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.

robertom’s picture

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
FileSize
11.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.

robertom’s picture

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.

quicksketch’s picture

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.

Cyberwolf’s picture

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).

Cyberwolf’s picture

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

Cyberwolf’s picture

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.

rickmanelius’s picture

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.

sandeshyadav’s picture

@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 ?

quicksketch’s picture

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.

quicksketch’s picture

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.

katharined206’s picture

Issue summary: View changes

Is it possible to edit the language of an error not related to required fields? For example, with an invalid credit card number, my form produces a un-readable jumble.

DanChadwick’s picture

@katharined206 - Let's keep this issue on topic -- customizing the required message. I have no idea what is causing your issue. You might try the support forums or IRC. If you don't get an answer, please start a new issue with detailed instructions about your language configuration, modules, and component type (there is no webform core "credit card number" component) and maybe someone can help you here.

DanChadwick’s picture

DanChadwick’s picture

Status: Needs work » Closed (won't fix)

Closing for lack of activity and demand.

abarpetia’s picture

Hello,
I think this is nice feature and this should be implemented. I have created one patch base on @Cyberwolf #17 and @quicksketch #22 comment. Please take a look.

- 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)

This patch will render required_error and unique_error field which will only visible when their respective checkbox is enabled.

- 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.

I am not sure how this will work but I'll appreciate if someone give me more information regarding this.

P.S. I tested this patch on all field but couldn't able to get it working on file field. I tried creating if condition same as textfield but it is still not working. Will appreciate if someone else with higher developing skills help me with this.

Thanks.

venkatesha.k12’s picture

Great patch.

@abarpetia, thanks a lot. saved my time.

+1 to commit on master.

Please let me know if i have to change the status.

Thanks.

drup16’s picture

Hi,

I have tried applying the patch from #26, but it has been unsuccessful. Can someone please let me know if I am missing a step?

1) Applied patch to both webform 7.x-4.12 and 7.x-4.13
2) Went to webform component and added validation message since now there is a new text field to enter it
3) cleared cache and refreshed the form
4) entered in a wrong value to force the validation message, but i do not get the same one that i entered under component text field mentioned in step 2

abarpetia’s picture

@raj.ruprai: That patch has been created against 7.x-4.x-dev so, could you please try using dev branch. Thanks

boby_ui’s picture

+1 its not working on the dev branch even though I added the patch?

Sam152’s picture

I've created a contrib module for this if anyone would prefer that over a patch: https://www.drupal.org/project/webform_required_message

andypost’s picture

jrockowitz’s picture

Custom required and unique error messages is already supported in D8.

andypost’s picture

@jrockowitz We hit a wall to make it work when error message should depend on "states" it looks like separate feature, just trying to figure out how to title the issue(

Basically required and could be changed depending on other form element so field may need separate validation message for each state

Right now it we made it as custom hack but wanna contribute somehow

Status: Needs review » Needs work
Juterpillar’s picture

abarpetia’s picture

Assigned: abarpetia » Unassigned
Status: Needs work » Needs review

@Juterpillar: Thanks for re-rolling patch.

Liam Morland’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
Matroskeen’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
16.52 KB

Here is a re-roll of #36 for 7.x-4.x-dev.