k-isabelle
5/31/2018 - 4:29 PM

Gravity Forms Custom Validation Function

Gravity Forms Custom Validation Function

// ==========================================
// * Gravity Forms Validate PIN
// ==========================================

// Source: http://www.gravityhelp.com/documentation/page/Using_the_Gravity_Forms_%22gform_validation%22_Hook

// 1 - Tie our validation function to the 'gform_validation' for all forms
// add_filter('gform_validation', 'validate_pin');

// 1 - Tie our validation function to the 'gform_validation' hook only if the form ID is 2
add_filter('gform_validation_2', 'validate_pin');

function validate_pin($validation_result) {

    // 2 - Get the form object from the validation result
    $form = $validation_result["form"];

    // 3 - Get the current page being validated
    $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;

    // 4 - Loop through the form fields
    foreach($form['fields'] as &$field){

        // 5 - If the field does not have our designated CSS class, skip it
        if(strpos($field['cssClass'], 'validate-pin') === false)
          continue;

        // 6 - Get the field's page number
        $field_page = $field['pageNumber'];

        // 7 - Check if the field is hidden by GF conditional logic
        $is_hidden = RGFormsModel::is_field_hidden($form, $field, array());

        // 8 - If the field is not on the current page OR if the field is hidden, skip it
        if($field_page != $current_page || $is_hidden)
          continue;

        // 9 - Get the submitted value from the $_POST
        $field_value = rgpost("input_{$field['id']}");

        // 10 - Make a call to your validation function to validate the value
        $is_valid = is_matching_pin($field_value, $form);

        // 11 - If the field is valid we don't need to do anything, skip it
        if($is_valid)
          continue;

        // 12 - The field field validation, so first we'll need to fail the validation for the entire form
        $validation_result['is_valid'] = false;

        // 13 - Next we'll mark the specific field that failed and add a custom validation message
        $field['failed_validation'] = true;
        $field['validation_message'] = 'PIN numbers don\'t match.';

    }

    // 14 - Assign our modified $form object back to the validation result
    $validation_result['form'] = $form;

    // 15 - Return the validation result
    return $validation_result;
}


function is_matching_pin($matching_pin, $form){

  $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;

  // 1. Loop through the form fields
  foreach($form['fields'] as &$field){

    // 2. If the field does not have our designated CSS class, skip it
    if(strpos($field['cssClass'], 'pin-to-match') === false)
      continue;

    // 3. Get the field's page number
    $field_page = $field['pageNumber'];

    // 4. Check if the field is hidden by GF conditional logic
    $is_hidden = RGFormsModel::is_field_hidden($form, $field, array());

    // 5. If the field is not on the current page OR if the field is hidden, skip it
    if($field_page != $current_page || $is_hidden)
      continue;

    // 6. Get the submitted value from the $_POST
    $pin_to_match = rgpost("input_{$field['id']}");

  }

  // $return['pin_to_match'] = $pin_to_match;
  // $return['matching_pin'] = $matching_pin;

  // 7. Check if the value matches our other value & return true or false
  if($pin_to_match == $matching_pin){
    $return = true;
  }
  else{
    $return = false;
  }

  return $return;

}


////////////////////////////////////////////////////////






// ==========================================
// * Gravity Forms Validation
// ==========================================


// 1 - Tie our validation function to the 'gform_validation' hook
add_filter('gform_validation', 'validate_pass');
add_filter('gform_validation', 'validate_pin');

function validate_pass($validation_result) {

    // 2 - Get the form object from the validation result
    $form = $validation_result["form"];

    // 3 - Get the current page being validated
    $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;

    // 4 - Loop through the form fields
    foreach($form['fields'] as &$field){

        // 5 - If the field does not have our designated CSS class, skip it
        if(strpos($field['cssClass'], 'validate-pass') === false)
          continue;

        // 6 - Get the field's page number
        $field_page = $field['pageNumber'];

        // 7 - Check if the field is hidden by GF conditional logic
        $is_hidden = RGFormsModel::is_field_hidden($form, $field, array());

        // 8 - If the field is not on the current page OR if the field is hidden, skip it
        if($field_page != $current_page || $is_hidden)
          continue;

        // 9 - Get the submitted value from the $_POST
        $field_value = rgpost("input_{$field['id']}");

        // 10 - Make a call to your validation function to validate the value
        $is_valid = is_matching_pass($field_value, $form);

        // 11 - If the field is valid we don't need to do anything, skip it
        if($is_valid)
          continue;

        // 12 - The field field validation, so first we'll need to fail the validation for the entire form
        $validation_result['is_valid'] = false;

        // 13 - Next we'll mark the specific field that failed and add a custom validation message
        $field['failed_validation'] = true;
        $field['validation_message'] = 'Your passwords don\'t match.';

    }

    // 14 - Assign our modified $form object back to the validation result
    $validation_result['form'] = $form;

    // 15 - Return the validation result
    return $validation_result;
}


function is_matching_pass($matching_pass, $form){

  $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;

  // 1. Loop through the form fields
  foreach($form['fields'] as &$field){

    // 2. If the field does not have our designated CSS class, skip it
    if(strpos($field['cssClass'], 'pass-to-match') === false)
      continue;

    // 3. Get the field's page number
    $field_page = $field['pageNumber'];

    // 4. Check if the field is hidden by GF conditional logic
    $is_hidden = RGFormsModel::is_field_hidden($form, $field, array());

    // 5. If the field is not on the current page OR if the field is hidden, skip it
    if($field_page != $current_page || $is_hidden)
      continue;

    // 6. Get the submitted value from the $_POST
    $pass_to_match = rgpost("input_{$field['id']}");

  }

  // 7. Check if the value matches our other value & return true or false
  if($pass_to_match == $matching_pass){
    $return = true;
  }
  else{
    $return = false;
  }
  return $return;
}


////////////////////////////////////////////////////////


function validate_pin($validation_result) {

    // 2 - Get the form object from the validation result
    $form = $validation_result["form"];

    // 3 - Get the current page being validated
    $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;

    // 4 - Loop through the form fields
    foreach($form['fields'] as &$field){

        // 5 - If the field does not have our designated CSS class, skip it
        if(strpos($field['cssClass'], 'validate-pin') === false)
          continue;

        // 6 - Get the field's page number
        $field_page = $field['pageNumber'];

        // 7 - Check if the field is hidden by GF conditional logic
        $is_hidden = RGFormsModel::is_field_hidden($form, $field, array());

        // 8 - If the field is not on the current page OR if the field is hidden, skip it
        if($field_page != $current_page || $is_hidden)
          continue;

        // 9 - Get the submitted value from the $_POST
        $field_value = rgpost("input_{$field['id']}");

        // 10 - Check if both PIN numbers match
        $is_matching = is_matching_pin($field_value, $form);

        // 11 - Check if PIN number is valid (4-digit whole number)
        $is_valid = is_valid_pin($field_value);

        // 11 - If the field is valid and matching we don't need to do anything, skip it
        if($is_matching && $is_valid['length'] && $is_valid['numeric'])
          continue;

        // 12 - The field field validation, so first we'll need to fail the validation for the entire form
        $validation_result['is_valid'] = false;

        // 13 - Next we'll mark the specific field that failed and add a custom validation message
        $field['failed_validation'] = true;

        if(!$is_matching){
          $field['validation_message'] = 'PIN numbers don\'t match.';
        }
        elseif(!$is_valid['numeric']){
          $field['validation_message'] = 'Your PIN number may only contain numeric digits.';
        }
        elseif(!$is_valid['length']){
          $field['validation_message'] = 'Please choose a PIN number that is exactly 4 digits in length.';
        }
    }

    // 14 - Assign our modified $form object back to the validation result
    $validation_result['form'] = $form;

    // 15 - Return the validation result
    return $validation_result;
}



function is_matching_pin($matching_pin, $form){

  $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;

  // 1. Loop through the form fields
  foreach($form['fields'] as &$field){

    // 2. If the field does not have our designated CSS class, skip it
    if(strpos($field['cssClass'], 'pin-to-match') === false)
      continue;

    // 3. Get the field's page number
    $field_page = $field['pageNumber'];

    // 4. Check if the field is hidden by GF conditional logic
    $is_hidden = RGFormsModel::is_field_hidden($form, $field, array());

    // 5. If the field is not on the current page OR if the field is hidden, skip it
    if($field_page != $current_page || $is_hidden)
      continue;

    // 6. Get the submitted value from the $_POST
    $pin_to_match = rgpost("input_{$field['id']}");

  }

  // $return['pin_to_match'] = $pin_to_match;
  // $return['matching_pin'] = $matching_pin;

  // 7. Check if the value matches our other value & return true or false
  if($pin_to_match == $matching_pin){
    $return = true;
  }
  else{
    $return = false;
  }

  return $return;

}



function is_valid_pin($pin){
  if(ctype_digit($pin)){
    $valid['numeric'] = true;
  }else{
    $valid['numeric'] = false;
  }
  if(strlen($pin) == 4){
    $valid['length'] = true;
  }
  else{
    $valid['length'] = false;
  }
  return $valid;
}

////////////////////////////////////////////////////////


// add_filter('gform_validation', 'validate_years_licensed');

function validate_years_licensed($validation_result) {

    // 2 - Get the form object from the validation result
    $form = $validation_result["form"];

    // 3 - Get the current page being validated
    $current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1;

    // 4 - Loop through the form fields
    foreach($form['fields'] as &$field){

        // 5 - If the field does not have our designated CSS class, skip it
        if(strpos($field['cssClass'], 'years-licensed') === false)
          continue;

        // 6 - Get the field's page number
        $field_page = $field['pageNumber'];

        // 7 - Check if the field is hidden by GF conditional logic
        $is_hidden = RGFormsModel::is_field_hidden($form, $field, array());

        // 8 - If the field is not on the current page OR if the field is hidden, skip it
        if($field_page != $current_page || $is_hidden)
          continue;

        // 9 - Get the submitted value from the $_POST
        $field_value = rgpost("input_{$field['id']}");

        // 10 - Check if PIN number is valid (4-digit whole number)
        $is_valid = is_valid_num_years($field_value);

        // 11 - If the field is valid we don't need to do anything, skip it
        if($is_valid['numeric'] && $is_valid['length'])
          continue;

        // 12 - The field failed validation, so first we'll need to fail the validation for the entire form
        $validation_result['is_valid'] = false;

        // 13 - Next we'll mark the specific field that failed and add a custom validation message
        $field['failed_validation'] = true;

        if(!$is_valid['numeric']){
          $field['validation_message'] = 'Please enter a numerical value.';
        }
        elseif(!$is_valid['length']){
          $field['validation_message'] = 'Please enter a 1-2 digit numerical value.';
        }
        
    }

    // 14 - Assign our modified $form object back to the validation result
    $validation_result['form'] = $form;

    // 15 - Return the validation result
    return $validation_result;
}


function is_valid_num_years($num_years){
  if(ctype_digit($num_years)){
    $valid['numeric'] = true;
  }else{
    $valid['numeric'] = false;
  }
  if(strlen($num_years) <= 2){
    $valid['length'] = true;
  }
  else{
    $valid['length'] = false;
  }
  return $valid;
}