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

nevets’s picture

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 like

$temp_str = preg_replace('/\s+/', '', $form_values['firstname']);
$len1 = strlen($temp_str);
$value = $form_values['firstname'];
$len2 = strlen($value);
drupal_sset_message("}$temp_str|$len1|$value|$len2|");

Also 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.

pratyk’s picture

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 ...
----------------------------------------------------------------------------------------------------------------------------------

<?php

function yes_alumni_info_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('The yes_alumni_info module adds activites to fm db.');
  }
}

function yes_alumni_info_perm() {
  return array('yesalumnform');
}

function yes_alumni_info_menu($may_cache) {
  	$items = array();

    $access = user_access('yesalumniform');
   
    if (!$may_cache) {

  	  $items[] = array('path' => 'yesalumni', 'title' => t('YES Alumni'),
      'callback' => yes_alumni_info_sampleform, 'access' => $access);
	  
	  $items[] = array('path' => 'yes_alumni_info/complete', 'title' => t('Thank You for Registering'),
      'type' => MENU_CALLBACK, 'callback' => yes_alumni_info_complete, 'access' => $access);
    
          $items[] = array('path' => 'yes_alumni_info/error', 'title' => t('Temporary Registration Error'),
      'type' => MENU_CALLBACK, 'callback' => yes_alumni_info_error, 'access' => $access);
    
    }
        
    return $items;
}

function yes_alumni_info_sampleform($username = false) { 
	global $user;

	$yesdb = yes_alumni_info_connect_to_fm('yesweb');
	$layout = $yesdb->FMView();
	$layout['valueLists'] = fixFXValueLists($layout['valueLists']);
			
	$states=array();

	if(isset($_GET['view'])){                     //from a different form ... 
		$email = $_GET['uname'];
		$iearnid = $_GET['duid'];
		$view = $_GET['view'];
		$yesdb->AddDBParam(xyz::email',$email);
		$yesdb->AddDBParam('xyz::iearnid',$iearnid);
		$AddUserData = $yesdb->FMFind();
	}else{ 
		$view='edit';
		$yesdb->AddDBParam('xyz::userid',$user->name);
		$yesdb->AddDBParam('xyz::drupal_uid',$user->uid);
		$AddUserData = $yesdb->FMFind();
	}
	$recidd = array_shift(explode('.',array_shift(array_keys($AddUserData['data']))));          //current record id

// check this portion .. maybe the error is in here .. 

	foreach ($AddUserData['data'] as $key=>$value){
		if($value['firstname'][0]=='' && $view=='view')
			$arr->firstname='&nbsp';
		else
			$arr->firstname= $value['firstname'][0];
			
		if($value['lastname'][0]=='' && $view=='view')
			$arr->lastname='&nbsp';
		else
			$arr->lastname=$value['lastname'][0];
			
		if($value['email'][0]=='' && $view=='view')
			$arr->email='&nbsp';
		else
			$arr->email=$value['email'][0];
			
		if($value['password'][0]=='' && $view=='view')
			$arr->password='&nbsp';
		else
			$arr->password=$value['password'][0];

		if($value['home_telephone'][0]=='' && $view=='view')
			$arr->homepnum='&nbsp';
		else
			$arr->homepnum=$value['home_telephone'][0];
			
		if($value['home_address'][0]=='' && $view=='view')
			$arr->homeaddress='&nbsp';
		else
			$arr->homeaddress=$value['home_address'][0];
		
		if($value['home_city'][0]=='' && $view=='view')
			$arr->homecity='&nbsp';
		else
			$arr->homecity=$value['home_city'][0];
		
		if($value['home_state'][0]=='' && $view=='view')
			$arr->homestate='&nbsp';
		else
			$arr->homestate=$value['home_state'][0];
			
		if($value['home_zip_code'][0]=='' && $view=='view')
			$arr->homezip='&nbsp';
		else
			$arr->homezip=$value['home_zip_code'][0];
			
		if($value['home_country'][0]=='' && $view=='view')
			$arr->homecountry='&nbsp';
		else
			$arr->homecountry=$value['home_country'][0];
		
		if($value['university'][0]=='' && $view=='view')
			$arr->university='&nbsp';
		else
			$arr->university=$value['university'][0];
		}
	
 	$form = array('Page1'=>array(),
	);

	$type='textfield';$type1='password';$type2='textarea';
	
	if($view=='view')
		{
		$type='fieldset';$type1='fieldset';$type2='fieldset';
		}
				
$form['Page1']['recid'] = array(
    '#type' => 'hidden',
    '#default_value' =>  $recidd);
    
    $form['Page1']['view'] = array(
    '#type' => 'hidden',
    '#default_value' =>  $view);
    
	$form['Page1']['firstname'] = array(
    '#type' => $type,
  	'#size' => 20,
    '#maxlength' => 24,
    '#value' =>  $arr->firstname, 
    '#title' => t('Firstname'),
    '#prefix' => '<table><tr><td width="250">', 
    '#suffix' => '</td>');
    	
	$form['Page1']['lastname'] = array(
    '#type' => $type, 
    '#size' => 20,
    '#maxlength' => 24,
    '#title' => t('Lastname'),
    '#value' =>  $arr->lastname,
    '#prefix' => '<td width="250">', 
    '#suffix' => '</td>');
    
    $form['Page1']['email'] = array(
    '#type' => $type,
    '#size' => 30,
    '#maxlength' => 34,
    '#prefix' => '<td width="250">',
    '#suffix' => '</td></tr></table>',
    '#title' => 'Email',
    '#value' => $arr->email);
    
    $form['Page1']['password'] = array(
    '#type' => $type1,
    '#size' => 20,
    '#maxlength' => 15,
    '#title' => 'New Password',
    '#value' => $arr->password);
    
    $form['Page1']['homeaddress'] = array(
    '#type' => $type,
    '#size' => 50,
    '#maxlength' => 54,
    '#value' =>  $arr->homeaddress, 
    '#title' => t('Home Address'),
    '#prefix' => '<table><tr><td width="350">', 
    '#suffix' => '</td>');
    	
	$form['Page1']['homecity'] = array(
    '#type' => $type,
    '#size' => 10,
    '#maxlength' => 14,    
    '#prefix' => '<td width="150">', 
    '#suffix' => '</td>',
    '#title' => t('Home City'),
    '#value' =>  $arr->homecity);
    
    $form['Page1']['homestate'] = array(
    '#type' => $type,
    '#size' => 10,
    '#maxlength' => 14,
    '#prefix' => '<td width="150">',
    '#suffix' => '</td></tr></table>',
    '#title' => t('Home State'),
    '#value' => $arr->homestate);
    
    $form['Page1']['homezip'] = array(
    '#type' => $type,
  	'#size' => 20,
    '#maxlength' => 24,
    '#value' =>  $arr->homezip, 
    '#title' => t('Home Postal Code'),
    '#prefix' => '<table><tr><td width="250">', 
    '#suffix' => '</td>');
    	
 	$form['Page1']['homecountry'] = array(
    '#type' => $type, 
    '#title' => t('Home Country'),
    //'#options'=> $countries, 
    '#value' =>  $arr->homecountry,
    '#prefix' => '<td width="250">', 
    '#suffix' => '</td></tr></table>');
  
	$form['Page1']['homepnum'] = array(
    '#type' => $type, 
    '#size' => 20,
    '#maxlength' => 24,
    '#title' => t('Home Phone Number'),
    '#value' =>  $arr->homepnum,
    '#prefix' => '<table><td><td width="250">', 
    '#suffix' => '</td></tr></table>');
    
    $form['Page1']['university'] = array(
    '#type' => $type, 
    '#size' => 40,
    '#maxlength' => 40,
    '#title' => t('University I\'m attending or attended'),
    '#value' =>  $arr->university,
    '#prefix' => '<table><tr><td width="350">', 
    '#suffix' => '</td></tr></table>');

 if ($view=='edit')
		$form['Page1']['submit'] = array('#type' => 'submit', '#value' => t('Update Information'));
	else
		; 
		
   	return drupal_get_form('yes_alumni_info_sample', $form);
}

function yes_alumni_info_sample_validate($form_id, $form_values){
	drupal_set_message( '<pre>' . print_r($form_values, TRUE) . '</pre>');

	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);
	}
}

function yes_alumni_info_connect_to_fm($layout) {
	$serverIP = 'x.x.x.x';
	$webCompanionPort = 80;         // for FM7SA, this should we the web server port
	$dataSourceType = 'FMPro7';
	$webUN = '123';               // defaults for fmfile in FM7; both should be blank for Book_List in FM5/6
	$webPW = '123';
    $FMFile = 'xyz.fp7';
	$scheme = 'http';               // generally this will be 'http'; 'https' for SSL connections to FileMaker

    $fm = new FX($serverIP, $webCompanionPort, $dataSourceType);     // This line creates an instance of the FXclass
	$fm->SetDBData($FMFile, $layout);                                                                                                     
	$fm->SetDBUserPass ($webUN, $webPW);               // Set the user name and password for the desired access.
	return $fm;
}

function yes_alumni_info_sample_submit($form_id, $form_values) {
		$YesEdit = yes_alumni_info_connect_to_fm('yesweb');
		$YesEdit->AddDBParam('firstname',$form_values['firstname']);
		$YesEdit->AddDBParam('lastname',$form_values['lastname']);
		$YesEdit->AddDBParam('password',$form_values['password']);
		$YesEdit->AddDBParam('home_telephone',$form_values['homepnum']);
		$YesEdit->AddDBParam('home_address',$form_values['homeaddress']);
		$YesEdit->AddDBParam('home_city',$form_values['homecity']);
		$YesEdit->AddDBParam('home_state',$form_values['homestate']);
		$YesEdit->AddDBParam('home_postal_code',$form_values['homezip']);
		$YesEdit->AddDBParam('home_country',$form_values['homecountry']);
		$YesEdit->AddDBParam('university',$form_values['university']);

		$YesEdit->AddDBParam('-recid',$recidd);
		
		if ($form_values['view']=='edit')	
			$result = $YesEdit->FMEdit();
		else
			;		
		return 'yes_alumni_info/complete/';
}
function yes_alumni_info_complete() {
	return file_get_contents('modules/yes_alumni_info/thank_you.html');
}
function yes_alumni_info_error() {
	return file_get_contents('modules/yes_alumni_info/error.html');
}
?>
nevets’s picture

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.

pratyk’s picture

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 ...

nevets’s picture

You need to print $messages in page.tpl.php, generally some where before $content though that is not required.