Postcode validation & entries [RESOLVED]

Hi,

I currently have a form that checks if a certain postcode is in the serviceable area of the business. See code below:

add_filter( 'gform_field_validation_2_8', 'custom_zip_validation', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] ) {

        $acceptable_zips = array('1234','3332','4444','5555',);
 
        $zip_value = rgar( $value, $field->id . '.5' );
 
        if ( ! in_array( $zip_value, $acceptable_zips ) ) {
       
			wp_redirect( get_permalink( 52 ) ); 

            exit();
        }		
    }
    return $result;
}

When a postcode isn’t in the serviceable area, the user is redirected to a page telling them so. When the postcode is in the serviceable area, it goes to the confirmation page, which has another form where the user can fill out further details.

The form currently submits the entry when the postcode is in the serviceable area. But I also need it to submit an entry when its not in the serviceable area, so the business can still get the lead, and act on it in future if the postcode becomes serviceable.

Is this possible? I’d appreciate any help with a solution and my code above. Thank you!

1 Like

If you need an entry either way, then you won’t be able to use gform_field_validation. That will prevent an entry when the postcode is outside the serviceable area. You will need to rethink how this works.

I recommend using the gform_confirmation filter to determine when a visitor should be directed to one page or another, based on your rules here on whether or not the postcode is in the serviceable area or not.