Ask your WordPress questions! Pay money and get answers fast! (more info)

Gravity Forms Validation

  • REFUNDED

Hi,

I require x2 tasks in Gravity Forms.

We deliver products to home addresses Monday, Fridays & Saturdays.
I have separated the postcodes into x4 arrays - $friday, $saturday, $frisat (Friday & Saturday) & $monday.

<strong>Task 1 </strong>
Validate form postcode/zipcode (Input ID=input_9_5_5) with the values below as they're the only postcodes we deliver to.

<strong>Task 2 </strong>
I need to populate a value into Field ID 3 after the postcode is inputted. (This will be hidden from customer)
So if enter postcode 3134. Field ID 3 will display number 1, then I can do conditional logic to display a radio button so they can select a delivery date.
Or if I enter
$friday = 1
$saturday = 2
$frisat = 3
$monday = 4

<strong>Postcode/Zipcode</strong>
$friday = array(
3134, 3135, 3136, 3153, 3154, 3155, 3156, 3157, 3158, 3159, 3160, 3196, 3197, 3198, 3199, 3200, 3201, 3219, 3222, 3223, 3224, 3225, 3226, 3227, 3228, 3229, 3230, 3231, 3232, 3235, 3236, 3239, 3243, 3759, 3765, 3766, 3767, 3768,
3769, 3770, 3771, 3772, 3773, 3774, 3775, 3776, 3777, 3781, 3782, 3783, 3784, 3785, 3786, 3787, 3788, 3789, 3790, 3791, 3792, 3793, 3794, 3795, 3796, 3800, 3801, 3802, 3803, 3804, 3805, 3806, 3807, 3808, 3809, 3810, 3910, 3911,
3912, 3913, 3914, 3915, 3916, 3917, 3918, 3919, 3920, 3926, 3927, 3928, 3929, 3930, 3931, 3932, 3933, 3934, 3935, 3936, 3937, 3938, 3939, 3940, 3941, 3942, 3943, 3944, 3975, 3976, 3977, 3978);

$saturday = array(
3023, 3024, 3043, 3045, 3047, 3048, 3049, 3221, 3240, 3241, 3321, 3322, 3323, 3335, 3336, 3337, 3338, 3339, 3340, 3427, 3428, 3429, 3750, 3751, 3752, 3753, 3754, 3755, 3756);

$frisat = array(
3000, 3001, 3002, 3003, 3004, 3005, 3006, 3007, 3008, 3009, 3010, 3011, 3012, 3013, 3014, 3015, 3016, 3017, 3018, 3019, 3020, 3021, 3022, 3025, 3026, 3027, 3028, 3029, 3030, 3031, 3032, 3033, 3034, 3039, 3040, 3041, 3042, 3044,
3046, 3050, 3051, 3052, 3053, 3054, 3055, 3056, 3057, 3058, 3059, 3060, 3061, 3062, 3063, 3064, 3065, 3066, 3067, 3068, 3069, 3070, 3071, 3072, 3073, 3074, 3075, 3076, 3077, 3078, 3079, 3080, 3081, 3082, 3083, 3084, 3085, 3086,
3087, 3088, 3089, 3090, 3091, 3092, 3093, 3094, 3095, 3096, 3097, 3098, 3099, 3100, 3101, 3102, 3103, 3104, 3105, 3106, 3107, 3108, 3109, 3110, 3111, 3112, 3113, 3114, 3115, 3116, 3117, 3118, 3119, 3120, 3121, 3122, 3123, 3124,
3125, 3126, 3127, 3128, 3129, 3130, 3131, 3132, 3133, 3134, 3135, 3136, 3137, 3138, 3139, 3140, 3141, 3142, 3143, 3144, 3145, 3146, 3147, 3148, 3149, 3150, 3151, 3152, 3161, 3162, 3163, 3164, 3165, 3166, 3167, 3168, 3169, 3170, |
3171, 3172, 3173, 3174, 3175, 3176, 3177, 3178, 3179, 3180, 3181, 3182, 3183, 3184, 3185, 3186, 3187, 3188, 3189, 3190, 3191, 3192, 3193, 3194, 3195, 3202, 3203, 3204, 3205, 3206, 3207, 3208, 3209, 3210, 3211, 3212, 3213, 3214,
3215, 3216, 3217, 3218, 3220, 3242, 3249, 3250, 3251, 3252, 3253, 3254, 8001, 8002, 8003, 8004, 8005, 8006, 8007, 8008, 8009, 8010, 8011, 8012, 8045, 8051, 8060, 8061, 8066, 8069, 8070, 8071, 8102, 8103, 8107, 8108, 8111, 8120,
8205, 8383, 8386, 8388, 8390, 8393, 8394, 8396, 8399, 8627);

$monday = array(
3350, 3353, 3354, 3355, 3356, 3357);


Form is located here [[LINK href="https://wildlamb.com.au/test/"]][[/LINK]]

Answers (1)

2016-12-10

Kyle answers:

Try this for 1 and 2 (you have to add the css class 'postcode' to the zip field):



add_filter('gform_validation_5', 'validate_postcode');
function validate_postcode($validation_result) {

$form = $validation_result["form"];
$current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;
foreach($form['fields'] as &$field){
if(strpos($field['cssClass'], 'postcode') === false)
continue;

$field_page = $field['pageNumber'];
$is_hidden = RGFormsModel::is_field_hidden($form, $field, array());

if($field_page != $current_page || $is_hidden)
continue;

$field_value = rgpost("input_5.5");
$is_valid = is_postcode($field_value);
if($is_valid)
continue;


$validation_result['is_valid'] = false;
$field['failed_validation'] = true;
$field['validation_message'] = 'De postcode is niet correct ingevuld.';

}

$validation_result['form'] = $form;
return $validation_result;

}

function is_postcode($value) {

$is_valid = false;

$friday = array(
3134, 3135, 3136, 3153, 3154, 3155, 3156, 3157, 3158, 3159, 3160, 3196, 3197, 3198, 3199, 3200, 3201, 3219, 3222, 3223, 3224, 3225, 3226, 3227, 3228, 3229, 3230, 3231, 3232, 3235, 3236, 3239, 3243, 3759, 3765, 3766, 3767, 3768,
3769, 3770, 3771, 3772, 3773, 3774, 3775, 3776, 3777, 3781, 3782, 3783, 3784, 3785, 3786, 3787, 3788, 3789, 3790, 3791, 3792, 3793, 3794, 3795, 3796, 3800, 3801, 3802, 3803, 3804, 3805, 3806, 3807, 3808, 3809, 3810, 3910, 3911,
3912, 3913, 3914, 3915, 3916, 3917, 3918, 3919, 3920, 3926, 3927, 3928, 3929, 3930, 3931, 3932, 3933, 3934, 3935, 3936, 3937, 3938, 3939, 3940, 3941, 3942, 3943, 3944, 3975, 3976, 3977, 3978);

$saturday = array(
3023, 3024, 3043, 3045, 3047, 3048, 3049, 3221, 3240, 3241, 3321, 3322, 3323, 3335, 3336, 3337, 3338, 3339, 3340, 3427, 3428, 3429, 3750, 3751, 3752, 3753, 3754, 3755, 3756);

$frisat = array(
3000, 3001, 3002, 3003, 3004, 3005, 3006, 3007, 3008, 3009, 3010, 3011, 3012, 3013, 3014, 3015, 3016, 3017, 3018, 3019, 3020, 3021, 3022, 3025, 3026, 3027, 3028, 3029, 3030, 3031, 3032, 3033, 3034, 3039, 3040, 3041, 3042, 3044,
3046, 3050, 3051, 3052, 3053, 3054, 3055, 3056, 3057, 3058, 3059, 3060, 3061, 3062, 3063, 3064, 3065, 3066, 3067, 3068, 3069, 3070, 3071, 3072, 3073, 3074, 3075, 3076, 3077, 3078, 3079, 3080, 3081, 3082, 3083, 3084, 3085, 3086,
3087, 3088, 3089, 3090, 3091, 3092, 3093, 3094, 3095, 3096, 3097, 3098, 3099, 3100, 3101, 3102, 3103, 3104, 3105, 3106, 3107, 3108, 3109, 3110, 3111, 3112, 3113, 3114, 3115, 3116, 3117, 3118, 3119, 3120, 3121, 3122, 3123, 3124,
3125, 3126, 3127, 3128, 3129, 3130, 3131, 3132, 3133, 3134, 3135, 3136, 3137, 3138, 3139, 3140, 3141, 3142, 3143, 3144, 3145, 3146, 3147, 3148, 3149, 3150, 3151, 3152, 3161, 3162, 3163, 3164, 3165, 3166, 3167, 3168, 3169, 3170, |
3171, 3172, 3173, 3174, 3175, 3176, 3177, 3178, 3179, 3180, 3181, 3182, 3183, 3184, 3185, 3186, 3187, 3188, 3189, 3190, 3191, 3192, 3193, 3194, 3195, 3202, 3203, 3204, 3205, 3206, 3207, 3208, 3209, 3210, 3211, 3212, 3213, 3214,
3215, 3216, 3217, 3218, 3220, 3242, 3249, 3250, 3251, 3252, 3253, 3254, 8001, 8002, 8003, 8004, 8005, 8006, 8007, 8008, 8009, 8010, 8011, 8012, 8045, 8051, 8060, 8061, 8066, 8069, 8070, 8071, 8102, 8103, 8107, 8108, 8111, 8120,
8205, 8383, 8386, 8388, 8390, 8393, 8394, 8396, 8399, 8627);

$monday = array(
3350, 3353, 3354, 3355, 3356, 3357);

if( in_array( $value, $friday ) ){

$is_valid = true;
$field_3 = 1;

}elseif( in_array( $value, $saturday ) ){

$is_valid = true;
$field_3 = 2;

}elseif( in_array( $value, $frisat ) ){

$is_valid = true;
$field_3 = 3;

}elseif( in_array( $value, $monday ) ){

$is_valid = true;
$field_3 = 4;

}

if( $is_valid ){

$_POST["input_3"] = $field_3;

}

return $is_valid;

}


parksey18 comments:

Task 1 works, just not seeing any number from task 2 populate into the postcode.


Kyle comments:

The second part is going to be trickier. You may actually need to write JS instead.