By phildog on
I've added a bunch of new user profiles under: administer -> users -> configure -> profiles
If I want to do some simple data validation on these new fields, what is the most logical place to put my PHP code?
thanks!
I've added a bunch of new user profiles under: administer -> users -> configure -> profiles
If I want to do some simple data validation on these new fields, what is the most logical place to put my PHP code?
thanks!
Comments
Well, the fields that are
Well, the fields that are available and how they are validated is all in the profile.module file. If you want custom fields (and thus custom validation) then it'd probably be a good idea to put in a feature request on that module.
If you want to be really inventive you could come up with a scheme where someone can specify a custom regular expression for text fields that it would validate against. In any case - post your request as a feature request on the core drupal project since profile.module is a part of the core:
http://drupal.org/node/add/project_issue/drupal/feature
If you just want a quick fix you should probably just go in to profile.module and duplicate all the code for how it handles the URL field and modify the validation to fit whatever you want your field to be.
--
http://www.electroniclife.org/
hook_user()
you have a couple options:
- enhance profile.module so that it accepts custom validation routines. i would think that you could add a hook during validation so that a developer could write a function that does the validation.
- don't use profile.module for these fields. Implementing them yourself in hook_user(). Thats the hook that profile.module itself uses.
Step-by-step explanation on validating profile fields
Hello,
This is what I did to validate profile fields.
Case
I have a Country and State/Province dropdown in my user profile creation and edit list. Because of how I built the profile fields, the first item is <----> in the dropdown for country, and the State/Province country includes a couple of separators that break out the provinces for separate countries. This means that some of the options in the dropdown menus are there for information but should really not go in the database.
I wanted to validate the Country and State province field so that if a user selected one of those separators, the user would be prompted to choose a valid entry.
Implementation
I followed a three-step process:
In this case, ['Company'] is the Category in which I put the Country and State fields -- you would use the text you entered in the 'Category' field when you created the profile field. ['profile_country'] and ['profile_state'] are the names of the field I'm validating -- you would enter whatever Drupal's Profile fields screen shows you under "Name" in /admin/user/profile . ['#validate'] is an element in the Form API that instructs drupal to call the function you specify in the assignment when validating the field -- in this case, I use two different functions, sitehelper_profile_country_validate and sitehelper_profile_state_validate to handle the country and the state cases.
What I'm doing there is checking for the values I don't want (they're drop-downs, so anything else is Kosher as far as I'm concerned) and using form_set_error to tell drupal that the field specified as the first parameter is in error (so that it puts a red box around it) and the error message to display on the top. $element is what drupal passes to your validation function, which contains the specific form element you're validating.
Overall this was about 15 minutes worth of work, mostly in figuring out the form hierarchy.
Hope this helps.
juanfe
Help
I think you are the one who cal help me.
I have been posting this for so long and I know you can answer. I want create to two roles eg contestant and member. Member gender can select on both male and female listbox but on contestant female only.
Is it possible to validate this during registration? Pls help. thanks
Step-by-step explanation on validating profile fields
Hello,
This is what I did to validate profile fields.
Case
I have a Country and State/Province dropdown in my user profile creation and edit list. Because of how I built the profile fields, the first item is <----> in the dropdown for country, and the State/Province country includes a couple of separators that break out the provinces for separate countries. This means that some of the options in the dropdown menus are there for information but should really not go in the database.
I wanted to validate the Country and State province field so that if a user selected one of those separators, the user would be prompted to choose a valid entry.
Implementation
I followed a three-step process:
In this case, ['Company'] is the Category in which I put the Country and State fields -- you would use the text you entered in the 'Category' field when you created the profile field. ['profile_country'] and ['profile_state'] are the names of the field I'm validating -- you would enter whatever Drupal's Profile fields screen shows you under "Name" in /admin/user/profile . ['#validate'] is an element in the Form API that instructs drupal to call the function you specify in the assignment when validating the field -- in this case, I use two different functions, sitehelper_profile_country_validate and sitehelper_profile_state_validate to handle the country and the state cases.
What I'm doing there is checking for the values I don't want (they're drop-downs, so anything else is Kosher as far as I'm concerned) and using form_set_error to tell drupal that the field specified as the first parameter is in error (so that it puts a red box around it) and the error message to display on the top. $element is what drupal passes to your validation function, which contains the specific form element you're validating.
Overall this was about 15 minutes worth of work, mostly in figuring out the form hierarchy.
Hope this helps.
juanfe
juanfe: many thanks for this
juanfe: many thanks for this explanation!
And to add something for module developers newbies like me:
If you get his problem:
"This version is incompatible with the 6.1 version of Drupal core."
Just add this line somewhere in the modulename.info file, in this case in the sitehelper.info file
http://drupal.org/node/229940
I have been trying to make a
I have been trying to make a much simple validation module in Drupal 6.1 to dont let visitors register if they havent accepted the terms and conditions of the page. For that, I created a checkbox using profile module called "profile_acepto" that when checked the user is accepting terms and conditions. After that I have followed juanfe explenation step by step, but its not working.
My sitehelper.module file looks like this:
Still a visitor can register without cheking the terms and conditions checkbox.
I also get this errors when registering:
warning: Cannot modify header information - headers already sent by (output started at /home/nosfuimo/public_html/sites/all/modules/sitehelper/sitehelper.module:10) in /home/nosfuimo/public_html/includes/common.inc on line 141.
Any help is welcome
Possible you have a new line
Possible you have a new line after the closing php tag.
Code
Can someone post the code to make this work in 5.14?
Thanks
good
this is good procedure
This code does not work for Drupal 6.x
I copied the code above and it does not work for Drupal 6.9, I would appreciate someone posting it for Drupal 6.9
The module loads ok and it is recognized in the modules section of Drupal 6.9 but it wont validate custom profile fields.
Custom profile field validation for Drupal 6.9
This code will work for Drupal 6.9
This is the sitehelper.info
This is the sitehelper.module
Code Modification for Drupal 5.14
What are the mods to make this work for 5.14 in profile.module?
There are no API breaks among
There are no API breaks among 5.X versions And where this is concerned, only the name of the validation element in fapi changed that I remember (and the info file format) so the above would work by only chnaging that from #element_validate to #validate. -mf
Cod Modifications
Thanks for the heads up. Will give you mods a try.
problems
its creating problems with 6.11, is there any change required?
validation problem
it gives me this warning, when i tested it online, and i needed to refresh my page to go to that page.
for example, if i login into my site using user name and password it doesnt open, it shows a blank white page, and after i refresh it goes to my home page.
it was working fine on my localhost. what could be the problem?
wwarning: Cannot modify header information - headers already sent by (output started at /home/content/a/s/s/mysite/html/modules/Form validation/sitehelper.module:2) in /home/content/a/s/s/mysite/html/includes/session.inc on line 97.
warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in /home/content/a/s/s/mysite/html/includes/session.inc on line 100.
mysite= the name of my site.
Mmmmmmm ! Think using hook_user() is straight forward and easy
Cant you just implement hook_user() as follows :(. Works fine for me and think it is pretty easy
Thanks
I am finding that Sankatha is right, hook_user() is a lot easier than the above example.
$edit
Sankatha is correct, but for a small adaptation. It shouldn't be
$account['profile_user_second_email']but$edit['profile_user_second_email'], since you are validating form values, not the account.The former does work during registration, but will break when editing users (ie at user/1/edit), while the latter works in both cases.
Validation runs even on default account page
I'm finding that this validation posted by Robin also runs on the default account page -- where the users update their email address, change their password, and set timezone settings, among other things. If you find this happenening, add a conditional so that the validation only runs when the field is present; i.e.