I created a form to do some custom validation on the Postal_Code field that Location provides. The code is as follows

<?php
function location_extra_node_validate($node, $form, &$form_state) {
	//dsm($form);
	//print "<pre>";
	//print_r($form['field_item_location']['und'][0]['postal_code']);
	//print_r($form_state['values']['field_item_location']['und'][0]['postal_code']);
	//print "</pre>";
	
	// create a variable that is a little easier to work with
	$zip = $form_state['values']['field_item_location']['und'][0]['postal_code'];
	
	// let's check to see if the value is a 5 digit US zipcode
	if($match = preg_match("#[0-9]{5}#", $zip)) {
		// check to see if the zipcode is actually in the database
		$result = db_query('SELECT zip FROM zipcodes WHERE zip = :zip', array(':zip' => $zip));
		
		//count to see if result is 0, indicating an invalid zipcode
		$number_of_rows = $result->rowCount();
		
		//if $number_of_rows is 0, throw form_set_error()
		if($number_of_rows == 0) {
			form_set_error('postal_code', 'Your zip code was not found. Please make sure you entered a valid 5 digit zip US code.');
		}
	} else {
		form_set_error('postal_code', 'This is not a valid zip code. Please enter a 5 digit US zip code.');
	}
}
?>

The function works, but when the error is thrown by form_set_error, the postal_code form field is not highlighted. I am sure it's because I have the wrong argument 'postal_code.' Can someone tell me what the correct title is so that highlighting works correctly on an error?

Thanks

Comments

samhassell’s picture

I'm struggling with the same issue in 6.x using location cck... it should be something like

form_set_error('field_quote_location[0][additional', t("Please enter your suburb."));

i think..

ankur’s picture

Status: Active » Fixed

@Gastonia,

samhassel, is right. In your case, it should be


form_set_error('field_item_location[und][0][postal_code', t('This is not a valid zip code. Please enter a 5 digit US zip code.'));

Feel free to re-open this ticket if that doesn't fix it for you.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.