We'd like to able to require just the zip code field of an address. Is this possible, using the pre-built address field?
This is a form it would be good to do on:
http://www.anandala.org/free-book-chapters/
Thanks!
We'd like to able to require just the zip code field of an address. Is this possible, using the pre-built address field?
This is a form it would be good to do on:
http://www.anandala.org/free-book-chapters/
Thanks!
Ananda,
This is possible through a hook and a few lines of PHP, but it will require some PHP experience.
Hi Alex, thanks for your answer. That's great to know. I see now that the hooks are documented.
Looks like I would use gform_pre_submission to run a validation on the zip code and -- somehow?? -- stop the form?
You would actually need to use the gform_validation filter. Following is a simple example on how to fail a validation for the first field in the form. It should point you in the right direction
<?php
add_filter('gform_validation', 'custom_validation');
function custom_validation($validation_result){
// set the form validation to false
$validation_result["is_valid"] = false;
$form = $validation_result["form"];
// specify the first field to be invalid and provide custom validation message
$form["fields"][0]["failed_validation"] = true;
$form["fields"][0]["validation_message"] = "This field is invalid!";
// update the form in the validation result with the form object you modified
$validation_result["form"] = $form;
return $validation_result;
}
?>
Superb. Thanks, Alex.
Is there a complete list of filters anywhere?
A complete list of filters will be available with the new documentation that will launch along with the 1.5 final release once it is out of beta testing.
Hi all,
Thought I'd add a note for other n00bs like me:
1) when I first copied this example, I somehow got rid of the following line -- that will break things:
$form = $validation_result["form"];
2) to get access to the submitted data, you have to access it via the regular HTTP $_POST["input_name"] PHP server variable (where 'input_name' is probably 'input_1', etc.), b/c at this stage of processing, the $form does not seem to have the submitted values set.
so, my full, working test example is:
add_filter('gform_validation', 'custom_validation');
function custom_validation($validation_result){
$form = $validation_result["form"];
// if value of first field name != 'peter'
if( strcmp($_POST["input_1"],"peter")!=0 ){
// then mark this submission invalid
$validation_result["is_valid"] = false;
// mark the errant field [the first (0th) field]
$form["fields"][0]["failed_validation"] = true;
// and put in an explanatory message to the user on how to fix
$form["fields"][0]["validation_message"] = "This first field must have the value 'peter'.";
}
// update the form in the validation result with the form object you modified
$validation_result["form"] = $form;
return $validation_result;
}