By pratyk on
Hi,
I have a form validation function which basically looks for two things:
1) if any of the fields is blank
2) if any firstname/lastname contains a space
in the form, there are 7 fields ... first name, last name, email address, password, address, city, country.
Now, even if i have some data in the form, upon submission, it gives me an error saying 3 of the 7 (password, address, city) are blank and the first two contain a space.
Any idea on why is this occuring ??
I am pasting the code .. i doubt there's anything wrong with the code but nevertheless .. if it helps ..
thanks a lot ...
function yes_alumni_info_sample_validate($form_id, $form_values){
foreach($form_values as $field=>$value) {
if($value != '') continue;
switch($field) {
case 'firstname':
case 'lastname':
case 'email':
case 'password':
case 'homeaddress':
case 'homecity':
case 'homecountry':
form_set_error($field,t('The value for '.$field.' cannot be blank.'));
break;
}
}
if (strlen(preg_replace('/\s+/', '', $form_values['firstname'])) != strlen($form_values['firstname'])){
form_set_error('firstname', t('Please remove spaces from your firstname'));
form_set_error('firstname','',true);
}
if (strlen(preg_replace('/\s+/', '', $form_values['lastname'])) != strlen($form_values['lastname'])){
form_set_error('lastname', t('Please remove spaces from your lastname'));
form_set_error('lastname','',true);
}
if ($form_values['homecountry']=='0'){
form_set_error('mail_country', t('Please Choose a country'));
form_set_error('mail_country','',true);
}
}
Comments
Some suggestions
It may have to do with how the form is structure and if you have any tree's ('#tree' => TRUE). Somethings I would try,
At the start of the function add
drupal_set_message( '<pre>' . print_r($form_values, TRUE) . '</pre>');to see what you are getting form form values. As for the first and last name checks. I would break them down so I could print the results of each of the operations to see what was happening. Something likeAlso I do not find a version in the API of form_set_error that takes a third parameter.
And a social comment, around here there are people with both first and last names that have spaces.
Thanks .. will try it out ...
i don't have any tree's in my form structure ..
basically it works in 2 modes : view and edit ..
if it is in the view mode (watching one else's profile) , the values are fetched from the database and displayed (form style fieldset). . .
if in the edit mode, the user gets the forms where he/she can enter values and update the information in the database (form style textfield/textarea/password)...
if it is possible, i was wondering if i you could view the module file in entirety so that you can have the complete picture ... if that is ok ..
as far as using the drupal set message feature, i am not seeing the message anywhere on the page ...
----------------------------------------------------------------------------------------------------------------------------------
On drupal_set_message
Your theme needs to print $messages for it to show up.
As for the code, I do not see anything obvious but it is a pretty non-traditional use of forms so I may have missed something.
regarding theme ...
newbie question but how do i get my theme to print $messages :) .. if you could provide a link to the process, it would be great ...
thanks again ...
It would go in page.tpl.php
You need to print $messages in page.tpl.php, generally some where before $content though that is not required.