I could use some coder help.
I have a page manager panel that displays a view with exposed filters for Location/Proximity and displays results of stores, along with a gmap for all resulting locations.
This works like a champ. The problem is I need to validate the results to make sure a zip code is entered, otherwise the result set is every store and the map takes a lot of time and resources to render.
I am trying to validate on the submit button, but it appears that the submit button is validated even before it is clicked.
I am using this custom module code to try to validate the form:
<?php
/**
* Implementation of hook_form_alter()
* to add a title to the units of distance form element.
*/
function omegasub_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'views_exposed_form' && $form['#id'] == 'views-exposed-form-dealer-locator-proximity-search-panel-pane-1') {
// Add custom label to search units parameter
$form['distance']['search_units']['#title'] = 'Units';
// Add custom validation form for distance parameter
$form['submit']['#validate'][] = 'omegasub_validate';
//dpm($form);
// Other things I tried below
//$form['reset']['#limit_validation_errors'] = TRUE;
//$form['#validate'][] = 'omegasub_validate';
}
}
function omegasub_validate($form, &$form_state) {
// Check to see if postal_code is empty
if ($form['distance']['postal_code']['#value'] == '') {
dsm($form); //Why is this firing on initial page load???
form_set_error('postal_code', t('You must enter a Postal Code.'));
}
}
?>I expected the omegasub_validate function to only fire on the submit button, but I get the set_error message on initial page load. Yes the zipcode exposed filter is empty, but the submit button has not been clicked, so it should not display the error message.
I have a reset button too, so I have to account for that once I find out what the problem is with my code attempt.
I have been trying to solve this for days...literally.
Thanks!

Comments
It's possible that Views
It's possible that Views submits the form when first generating the results in order to account for the exposed filters when displaying the default results. I haven't confirmed that, but it might be a good thing to check. Don't know how to work around that off the top of my head. You could maybe compare $form_state['triggering_element'] on initial load and when hitting the submit button. This might be a condition you can use for whether or not to set the error.
Thanks for the suggestion
$form_state['triggering_element'] array and subarray values on initial load and after hitting the submit button are identical when diff'd.
James Sinkiewicz
Drupal Site Builder and Generalist
http://MyDrupalJourney.com
drupal_get_form
Howdy,
I am assuming you have cleared the form cache tables. Are you using d6 or d7?
Regarding "it appears that the submit button is validated even before it is clicked", can you elaborate. The hook_validate is indeed run when a form is initially built in FAPI, but I don't understand why the submit button being validated would have anything to do with this.
Other than that, I agree with @Chaulky, I would check the views exposed filter plugin and see what it is doing. A dpm/dsm in there wouldn't be a bad thing.
Making Progress
Again, thanks for the suggestions.
Since the URL is free of all GET values on form load, but full of them after form submit, I tried a different approach:
<?php
/**
* Implementation of hook_form_alter()
* to add a title to the units of distance form element.
*/
function omegasub_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'views_exposed_form' && $form['#id'] ==
'views-exposed-form-dealer-locator-proximity-search-panel-pane-1') {
// Add custom validation form for distance parametier
$form['distance']['search_units']['#title'] = 'Units';
//Send form error if blank Postal Code submitted
if (isset($_GET['distance']['postal_code']) && $_GET['distance']['postal_code'] == '') {
form_set_error('postal_code', t('Sorry, you must enter a Postal Code.'));
}
}
}
?>
The code above fixed my submit button problem, but now have to work ion the reset button firing the form error when it is clicked.
I can't seem to find a value to indicate what button was used to trigger the form rebuild.If I isolate that, maybe I could test to see if it was the reset button somehow and fix my reset button issue.
(***please note that I am on week one of an "intermediate" PHP class from a college extension course )
James Sinkiewicz
Drupal Site Builder and Generalist
http://MyDrupalJourney.com
The
The $form_state['triggering_element'] should have that information. It's meant to track what page element triggered the form submission/rebuild. I know you checked it's value in a previous suggestion when the form first loaded and when the submit button was pressed. But, what is the value of 'triggering_element' when you hit the Reset button?
Form State
The $form_state['triggering_element'] does say reset when reset is pressed, but it also says submit on page load before the submit button is pressed and also if submit button is pressed. My validation function is run on page load, even though I have ti set to run on submit. So form api must run a submit with exposed filters on ( or maybe all the time).
So I can't use the if ($form['distance']['postal_code']['#value'] == '') in the validation, because it is true on page load and if the submit button is pressed.
I tried setting validation to form validation instead of submit validation and it had not effect.
As far as I can tell, there is not value I can check to see if the form is an empty (initial) form load or a submitted form load.
James Sinkiewicz
Drupal Site Builder and Generalist
http://MyDrupalJourney.com
Are the exposed filters empty
Are the exposed filters empty when the view first loads? You mentioned the main problem you're trying to solve is to validate the zip code because otherwise all stores are returned and takes too long to load. But what's the initial state when the View is first loaded? Are there default values for the exposed filter?
What if you make the zip code field required, and on empty values for zip code display no results. Then you wouldn't need the validation to check for a non-empty value. If you wanted to validate the zip code value itself, you could first check that it's set in the validation function. That way the validation function won't throw up the error state for empty values but you're view won't initially load all of the stores.
I'm hoping I understand your requirements correctly and that this suggestion makes sense. Let me know if I'm misunderstanding or if I need to be more clear with the suggestion.
Solved - Kind of...
Thanks for all the suggestions.
I did have the zip code exposed filter set to required but I am not just using a stand alone zip code field, but the Location module Proximity/Distance filter which combines several options into one. This was set to required. This filter adds to the complexity of the problem. On top of that, I am displaying various parts of the form in several Panels panes.
I think this is a hack, but what I did to fix the reset button issue was to place a check before all other validation checks, to see if Reset was clicked, then called drupal_goto jump to the initial form load page.
This is my way of dealing with the reset issue...just start over!
<?phpif ($form_state['triggering_element']['#value'] == 'Reset') {
drupal_goto('dealer/locator/proximity');
?>
Thanks again for all the help!
James Sinkiewicz
Drupal Site Builder and Generalist
http://MyDrupalJourney.com
#rebuild
There should also be the use of the #rebuild FAPI key that you can set to true for the case of the reset. But if it works, it works
I had the #rebuild
But it made no difference, so I deleted it from the code.
Thanks!
James Sinkiewicz
Drupal Site Builder and Generalist
http://MyDrupalJourney.com