I am new to building modules. I have the Location module enabled, which provides a zip code field for my content type. Everything works great, except that on submit, I want to alter the functionality so that, when saved, the field is validated against the database to check if the zip code actually exists. If not, throws an error.

the validation code to check against the database works fine, I just dont know where to put it. Do I use hook_form_alter on the node form? to I plug it in somehow to work with the Location module validation code?

Any direction is appreciated. Thanks!

Comments

mayank-kamothi’s picture

Hi,

You can do this by alter node form by using hook_form_alter ,put this function in your custom module and than function is work with form-id

E.x

function hook_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_form') {    
    drupal_set_message(t('some message.'));
  }  
}

Mayank Kamothi

otmoroz095’s picture

could you say me, how to create form_alter for one type of node.
is it correct?:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_mytype_form') {   
    drupal_set_message(t('some message.'));
  } 
}

nevets’s picture

In this case it would be simpler to use hook_node_validate()

Gastonia’s picture

Thanks for the post. How can I determine the form structure so I can properly use 'form_set_error' I tried print_r $form_state.

PrabhuG’s picture

you can install devel module. and use dpm($form_state). And user need to administrator (id - 1)

nevets’s picture

Also, once you have devel installed the node will have a tab where you can examine it's structure.

Gastonia’s picture

I have successfully used hook_node_validate to do what I need to do. here is the code, in case there are any suggestions so far:

<?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 "</pre>";
	
	// create a variable that is a little easier to work with
	$zip = $form['field_item_location']['und'][0]['postal_code']['#value'];
	
	// 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 zip code not present
		$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('field_item_location[und][0][postal_code]', 'This is not a valid zip code. Please enter a 5 digit US zip code.');
	}
}
?>

Everything is working now, except for one small detail. Typically Drupal highlights the field that needs attention in red. My postal code field is not being highlighted in red, despite form_set_error properly disallowing the submit to continue. I realized that I do not have the correct field name for the first argument, and, if I did, it would turn red as well. My issue is I do not know how to find this name. There are SO MANY 'postal code' type fields to choose from when I run dsm($form).

I also learned that form_set_error does not care what this first argument is. I could put 'bobsplayland' there and the function would still work. Is that not a bug? Shouldn't it enforce a proper field name for this?

Regardless, I want my field to turn red. Can someone suggest how I can correctly find this out?

Thanks again!

nevets’s picture

This

    $zip = $form['field_item_location']['und'][0]['postal_code']['#value'];

should get the value for $form_state['values'].

And from your code the first arg to form_set_error() I think should be 'field_item_location[0][postal_code' or something like it.

And side note, your code does not allow for multiple locations.