Hello, I am wanting to create a zip code validation within a form for a subset of zips. I see how I can do this with an array but I would like to have an office manager add and remove zips as the business needs it so I want to create a way for the user to manage zips from within the WP dashboard.
My thought is to create a custom post type and a custom field. Custom Post Type will be active zips and include a title which will be the city and a custom field, zip code.
Is there a way for me to validate that the zip code entered by a user on a form is within the current active zip codes from that custom post type?
Sure, the same way that you do the custom validation with the array using the gform_field_validation filter. But in this case you need to create the code to query your custom post first and parse the data to use it for the custom validation.
Any chance you can help with that :)? It’s a little outside my know how. I’ve got the following so far which I put together from a few other posts:
//Custom post type to query is delivery_area
//Custom field to query zip_code
//I've currently assigned a custom field group with zip_code to the custom post type delivery_area
//ZIP CODE Validation for vessel creation form
add_filter( 'gform_field_validation_7_11', 'zip_validation_1', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
if ( $result['is_valid'] ) {
$acceptable_zips = array(
);
$zip_value = rgar( $value, $field->id . '.5' );
if ( ! in_array( $zip_value, $acceptable_zips ) ) {
$result['is_valid'] = false;
$result['message'] = 'Your location is not within our delivery area.';
}
}
return $result;
}
But still probably good to remember this is client side only, users can (not very easible though, and can’t fully test it now) still change values in browser inspector tools to still be able to submit the form.