Skip to content

Instantly share code, notes, and snippets.

@BronsonQuick
Created April 12, 2012 09:51

Revisions

  1. Bronson Quick revised this gist May 22, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gravity_forms_validation.php
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ function custom_validation($validation_result)
    //We need to loop through all the form fields to check if the value matches and of the default values
    foreach ($form['fields'] as &$field) {
    //Check if the value is equal to the Default Value you entered in Gravity Forms
    if ($field["value"] = "Your name") {
    if ($field["value"] === "Your name") {
    // set the form validation to false
    $validation_result["is_valid"] = false;
    //The field ID can be found by hovering over the field in the backend of WordPress
    @@ -25,7 +25,7 @@ function custom_validation($validation_result)
    $field["validation_message"] = "Please enter your name.";
    }
    }
    if ($field["value"] = "Job Title") {
    if ($field["value"] === "Job Title") {
    // set the form validation to false
    $validation_result["is_valid"] = false;
    //The field ID can be found by hovering over the field in the backend of WordPress
    @@ -34,7 +34,7 @@ function custom_validation($validation_result)
    $field["validation_message"] = "Please enter your job title.";
    }
    }
    if ($field["value"] = "Contact phone number") {
    if ($field["value"] === "Contact phone number") {
    // set the form validation to false
    $validation_result["is_valid"] = false;
    //The field ID can be found by hovering over the field in the backend of WordPress
    @@ -43,7 +43,7 @@ function custom_validation($validation_result)
    $field["validation_message"] = "Please enter your contact phone number.";
    }
    }
    if ($field["value"] = "Company") {
    if ($field["value"] === "Company") {
    // set the form validation to false
    $validation_result["is_valid"] = false;
    //The field ID can be found by hovering over the field in the backend of WordPress
  2. Bronson Quick revised this gist Apr 12, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gravity_forms_validation.php
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ function change_validation_message($message, $form)

    // Often forms in Gravity Forms to need to start with default values and we need to check the form in case the user hasn't entered new data and give them a validation message back

    // Append the Gravity Forms ID to the end of the filter so that the validation only runs on this form
    // Append the Gravity Forms ID to the end of the filter so that the validation only runs on this form. In this example I'm doing it on Form #2
    add_filter('gform_validation_2', 'custom_validation');
    function custom_validation($validation_result)
    {
  3. Bronson Quick created this gist Apr 12, 2012.
    59 changes: 59 additions & 0 deletions gravity_forms_validation.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    <?php
    //This is a filter to change the default validation message that Gravity Forms generates
    add_filter('gform_validation_message', 'change_validation_message', 10, 2);
    function change_validation_message($message, $form)
    {
    return "<div class='validation_error'><strong>Oops!</strong> Looks like there’s something wrong. Please review the form above.</div>";
    }

    // Often forms in Gravity Forms to need to start with default values and we need to check the form in case the user hasn't entered new data and give them a validation message back

    // Append the Gravity Forms ID to the end of the filter so that the validation only runs on this form
    add_filter('gform_validation_2', 'custom_validation');
    function custom_validation($validation_result)
    {
    $form = $validation_result["form"];
    //We need to loop through all the form fields to check if the value matches and of the default values
    foreach ($form['fields'] as &$field) {
    //Check if the value is equal to the Default Value you entered in Gravity Forms
    if ($field["value"] = "Your name") {
    // set the form validation to false
    $validation_result["is_valid"] = false;
    //The field ID can be found by hovering over the field in the backend of WordPress
    if ($field["id"] == "2") {
    $field["failed_validation"] = true;
    $field["validation_message"] = "Please enter your name.";
    }
    }
    if ($field["value"] = "Job Title") {
    // set the form validation to false
    $validation_result["is_valid"] = false;
    //The field ID can be found by hovering over the field in the backend of WordPress
    if ($field["id"] == "4") {
    $field["failed_validation"] = true;
    $field["validation_message"] = "Please enter your job title.";
    }
    }
    if ($field["value"] = "Contact phone number") {
    // set the form validation to false
    $validation_result["is_valid"] = false;
    //The field ID can be found by hovering over the field in the backend of WordPress
    if ($field["id"] == "5") {
    $field["failed_validation"] = true;
    $field["validation_message"] = "Please enter your contact phone number.";
    }
    }
    if ($field["value"] = "Company") {
    // set the form validation to false
    $validation_result["is_valid"] = false;
    //The field ID can be found by hovering over the field in the backend of WordPress
    if ($field["id"] == "6") {
    $field["failed_validation"] = true;
    $field["validation_message"] = "Please enter your company name.";
    }
    }
    }
    //Assign modified $form object back to the validation result
    $validation_result["form"] = $form;
    return $validation_result;
    }?>