Closed (fixed)
Project:
Webform
Version:
6.x-2.1
Component:
Code
Priority:
Normal
Category:
Support request
Assigned:
Reporter:
Created:
7 Jan 2008 at 13:28 UTC
Updated:
20 Jun 2011 at 10:27 UTC
I generated a form by using the Webform module.
But how can I add validation for the elements in the form?
p.s
As forms_api.html said, I tried to add some validation code within the Advanced Settings/Additional Validation.
e.g.
if ( !preg_match( '/^[a-zA-Z]+$/', $form_values['first_name']) ) {
form_set_error('', t('The First Name you entered is not validated.'));
}
}
or
if ( !preg_match( '/^[a-zA-Z]+$/', $first_name) ) {
form_set_error('', t('The First Name you entered is not validated.'));
}
}
I wanted to validate the element called first_name, but both codes did not work!
Comments
Comment #1
mandclu commentedI've been able to do what you want by writing a module that implements hook_form_alter to set a new '#validate' value, and perform the additional validation there.
Comment #2
jaojao commentedHow can i use the hook_form_alter function?
Could you write down the code within my case!
Comment #3
mandclu commentedOK, let's say the your form id is 'webform_client_form_2', you could create a module called myform:
Comment #4
jaojao commentedSince I generated the form by Webform automatically, how can I know my form id? Is the form id indicative of form title?
Comment #5
mandclu commentedIf you view the source (HTML) of the generated page, you should be able to get the form id.
Comment #6
jaojao commented"Create a module called myform", you mean to create a new myform.module document and to save it under the direct.: drupal/modules/myform ?
If it said so, what should I do next? How to make the concatenation between my form and the module?
Alternatively, is there any simply way to write validating code just within the Advanced Settings/Additional Validation section? I really don't know how to deal with the Additional Validation section, even after read the API manual!!!!!
Comment #7
manuel garcia commentedOK, so after several hours researching this (note that I am not a programmer), I think I may be able to help here;
I was trying to validate that the user entered values in EITHER one of three fields in the form, here is my code:
so basicaly it appears that in order to check the content of the field with field key "nombre", you have to call it by
$form_values['submitted_tree']['nombre']So my guess is that for your issue, the code would look something like this:
I haven't tested your code previously, so do let us know if it works for you now.
Also if someone could explain why the use of ['submitted_tree'] it would be nice to know :D
Greetings from Spain, and thanks for this great module!
Comment #8
jaojao commentedThank you for your information!!!!!!!
It works eventually in my case. I understand now how the tree is constructed in the Webform. :)
p.s. there is a little trick when you write two validations parallelly. The 2. note, "ZIP muss in nummer eingetragen werden!", doesn't appear until the 1. validation is passed. It means, the validation is processed node by node. It confused me at the first time I tested the form! :)
And the "root" ['submitted_tree'] is still unknown. I suppose, it is defined by Webform in advance.
Comment #9
manuel garcia commentedI'm glad my input got you through the issue jaojao! :)
Comment #10
manuel garcia commentedalthough it would be nice if someone documented this properly... :)
Comment #11
GBain22 commentedCheers guys, this has helped me so much. I have used this to help battle spam I was receiving through my webform. By adding in a question that the user had to type in to get right, then it processes the form. If anyone wants the code, just reply to me.
Thanks again, Garry.
Comment #12
devloper_sumi commentedThanx for ur guidance...
Using this $form_values['submitted_tree']['confirm_email'] i bcame able to add additional validation
Comment #13
td540 commentedHi,
I'm looking for a way to send the user to a page node that acts as confirmation message, based on whether or not they gave the right answer in a specific field.
If they fill in the right answer, and they go to the "correct" node page.
If answer is wrong, go to the "wrong" node page.
Comment #14
roczei commentedHi guys,
Here is a correct example for this issue, this is working in the validation field:
My suggestion for debugging, IMPORTANT file: includes\form.inc
I changed it a bit.... :P
This was before:
I changed it to this:
You can see that I added the ." ".$name thing, because it can be used for "form id" debugging.
Example for form id:
Why important to give the formid to form_set_error, because it will "add" color to the problematic field. Summary: use it. :)
Cheers,
Gabor
Comment #15
pavaniakella commentedHi
Can any one post an blue print not the example.
So that I can write my names into it to change the code.
Cheers,
Pavani
Comment #16
vsalvans commentedThanks a lot!
I tried dump variables by putting print_r into additional validation field but nothing appear.. I don't know how you figured out the name of the correct variable.
Moltes gràcies!
Comment #17
yan commentedThis module does it (now):
http://drupal.org/project/webform_validation