Required fields custom validation message

the custom validation message on required fields only dissaperas on pageload, meaning the validation message should be hidden WHEN the field is set… i tried this code snippet in functions.php

add_filter( 'gform_validation_message', 'henriette_gf_hide_validation_message', 10, 2 );

function henriette_gf_hide_validation_message( $message, $form ) {
  $is_valid = true;  // Flag to track validation status

  // Loop through all fields in the form
  foreach ( $form['fields'] as $field ) {
    if ( $field['required'] && empty( $_POST["input_{$field['id']}"]) ) {
      $is_valid = false;
      break;  // Exit loop if a required field is empty
    }
  }

  // If all required fields are filled, return an empty string (hides message)
  return $is_valid ? '' : $message;
}

did not work - then i tried this script

document.addEventListener("DOMContentLoaded", function() {
  const requiredFields = document.querySelectorAll(".gfield_required input, .gfield_required textarea");
  const validationMessage = document.querySelector(".gform_validation_message"); // Adjust selector if needed

  requiredFields.forEach(function(field) {
    field.addEventListener("input", function() {
      const hiddenField = document.getElementById("field_filled"); // Replace with your hidden field ID
      if (this.value.trim() !== "") {
        validationMessage.style.display = "none";
        hiddenField.value = "1"; // Update hidden field value
      } else {
        validationMessage.style.display = "block";
        hiddenField.value = "";
      }
    });
  });
});

Dis not work either…

Hi Henriette,

You can follow my previous response on another ticket, where you’ll find the proper solution.

Give it a try, and let me know how that goes! :smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.