By Gastonia on
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
Yes
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
Mayank Kamothi
node type?
could you say me, how to create form_alter for one type of node.
is it correct?:
In this case it would be
In this case it would be simpler to use hook_node_validate()
Thanks for the post. How can
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.
you can install devel module.
you can install devel module. and use dpm($form_state). And user need to administrator (id - 1)
Also, once you have devel
Also, once you have devel installed the node will have a tab where you can examine it's structure.
ok, getting closer
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:
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!
This $zip =
This
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.