Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
quicksketchI 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.
Comment #2
vernond CreditAttribution: vernond commentedPerhaps 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?
Comment #3
icosa CreditAttribution: icosa commented@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.
Comment #4
icosa CreditAttribution: icosa commented@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.
Comment #5
vernond CreditAttribution: vernond commentedCool. As you say, it's a bit of a wrench, but it can't be knocked too badly if it works I guess.
Comment #6
bevin CreditAttribution: bevin commentedI 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:
After Change,
This is a global change, whenever this particular required field required in varies forms, this customised message will show. :)
Comment #7
robertom CreditAttribution: robertom commentedI'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
Comment #8
robertom CreditAttribution: robertom commentedcheck also #1115386: Required and Fieldset Childs Required
Comment #9
quicksketchClosed #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:
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.
Comment #10
chaloum CreditAttribution: chaloum commentedWhile 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.
Comment #11
quicksketch@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.
Comment #12
robertom CreditAttribution: robertom commentedI 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.
Comment #13
robertom CreditAttribution: robertom commentedobviously, if needed, I could change patch for use #required_message and/or split patch in some little patches for make review more simple.
Comment #14
quicksketchNo 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.
Comment #15
Cyberwolf CreditAttribution: Cyberwolf commentedThe 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).
Comment #16
Cyberwolf CreditAttribution: Cyberwolf commentedShould have started from the master branch of course, will make another patch.
Comment #17
Cyberwolf CreditAttribution: Cyberwolf commentedNew 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.
Comment #18
rickmanelius CreditAttribution: rickmanelius commentedRelated 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.
Comment #19
sandeshyadav CreditAttribution: sandeshyadav commented@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 ?
Comment #20
quicksketchNew 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:
Still not sure how I feel about incorporating this into the UI.
Comment #21
quicksketchSo 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.
Comment #22
katharined206 CreditAttribution: katharined206 commentedIs 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.
Comment #23
DanChadwick CreditAttribution: DanChadwick commented@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.
Comment #24
DanChadwick CreditAttribution: DanChadwick commentedComment #25
DanChadwick CreditAttribution: DanChadwick commentedClosing for lack of activity and demand.
Comment #26
abarpetia CreditAttribution: abarpetia as a volunteer commentedHello,
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.
This patch will render required_error and unique_error field which will only visible when their respective checkbox is enabled.
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.
Comment #27
venkatesha.k12 CreditAttribution: venkatesha.k12 as a volunteer commentedGreat 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.
Comment #28
drup16 CreditAttribution: drup16 commentedHi,
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
Comment #29
abarpetia CreditAttribution: abarpetia as a volunteer commented@raj.ruprai: That patch has been created against 7.x-4.x-dev so, could you please try using dev branch. Thanks
Comment #30
boby_ui CreditAttribution: boby_ui as a volunteer commented+1 its not working on the dev branch even though I added the patch?
Comment #31
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedI've created a contrib module for this if anyone would prefer that over a patch: https://www.drupal.org/project/webform_required_message
Comment #32
andypostHow to deal with port for d8 #889198: Add capability to use custom error message for required field needs separate issue?
Really handy to have it
Comment #33
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedCustom required and unique error messages is already supported in D8.
Comment #34
andypost@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
Comment #36
Juterpillar CreditAttribution: Juterpillar at Catalyst IT commentedRe-rolling patch for 4.21
Comment #37
abarpetia CreditAttribution: abarpetia as a volunteer commented@Juterpillar: Thanks for re-rolling patch.
Comment #38
Liam MorlandComment #39
MatroskeenHere is a re-roll of #36 for 7.x-4.x-dev.