How to add a 'Please Choose' default value on a profile field? There's a quick workaround on this with CCK but it doesn't work on profile field. Anyone?

The field is required. Example would be gender field. So I have three selection on the list 'male', 'female', and 'please choose'. However if 'please choose' is left as is this value is going through and is being accepted.

Comments

tonicboy’s picture

This is an untested solution, but maybe you could alter that field in your theme template.php and ad an option "Please Choose" with a value of blank. Hopefully the blank will still be caught by the validator as an unprovided value. Or, you could create your own validator which does not accept "Please Choose" for required fields.

halloffame’s picture

Thank you for your little advice there. It actually helped me.

I end up using Regular Expression in validating my form field. Soooo much easier and its fun too! From now on ever I'm not going to have any problem in validating any form field on my site.

If anyone of you want to achieve the same thing (validating form in general) here are some few resources.

Validation API
Regular Expression Basic
Regular Expression Library All sorts of form validation! Very handy.

More advanced stuff:
http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...
http://api.drupal.org/api/function/hook_user/6

shakazam’s picture

Hi Evilgenius!

I want to do almost the same thing you did.

In fact I created the list selection field (Form name: field_gender, Title : Gender) with the profile module and add the same list: 'male', 'female', and 'please choose'. (both "The user must enter a value" and "Visible in user registration form" are checked)

What I want to do it's if one try to register without giving its gender ('Please choose' selected in fact), it would not be registered and an error message appears. It seems, according to your idea, that validation api is the right module to do this.

Here are things I've done with this module:

Step 1) Add a validator
Name : gender_validation
Type : Regex
Rule : ((male)|(female)) <- I'm really not sure about this. Do I have to provide an argument?
Message : %field must be selected

Step 2) Add a field
Form ID: profile_gender
Title : Gender
Validator : gender_validation
Allow this field to be empty? : unchecked

Unfortunatly it doesn't work... I would be pleased If you can help me on this
Many thanks

halloffame’s picture

The Regular Expression you want to use here is (Rule) "Male|Female" It's basically saying that only Male and Female values are being accepted and nothing else. In this case validation will only work when editing profile though.

If you want to invoke validation upon registering you need to use user_register as its Form ID and not profile_gender.

Let me know how it goes...