Last active
June 21, 2019 12:56
-
-
Save drzraf/8f3a5eba0ccd2d7f3eec689be64d2951 to your computer and use it in GitHub Desktop.
Gravity Forms HTML5 - custom validation message support (https://wordpress.org/support/topic/custom-validation-message-support-2/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/gravityforms-html5-validation.php b/gravityforms-html5-validation.php | |
index 5e2c9e0..116714c 100644 | |
--- a/gravityforms-html5-validation.php | |
+++ b/gravityforms-html5-validation.php | |
@@ -599,6 +599,12 @@ public function gform_field_content( $field_content, $field, $force_frontend_lab | |
break; | |
} | |
+ if ( $field->validation_message ) { | |
+ if ( $element = (( $result = $xpath->query( "//input" )) ? $result->item( 0 ) : null ) ) { | |
+ $element->setAttribute( 'data-validation-message', $field->validation_message ); | |
+ } | |
+ } | |
+ | |
$field_content = $doc->saveHTML(); | |
// Remove our html wrapper from processed field. | |
diff --git a/js/gravityforms-html5-validation.js b/js/gravityforms-html5-validation.js | |
index 6e66dab..917ef7c 100644 | |
--- a/js/gravityforms-html5-validation.js | |
+++ b/js/gravityforms-html5-validation.js | |
@@ -20,6 +20,7 @@ function( elem, i, match ) { | |
var debug = window.GFHtml5Validation.Debug = false; | |
var pageValidating = false; | |
var pageValid = false; | |
+ var validationMessage = false; | |
var trace = function( message, target ) { | |
if ( debug ) { | |
@@ -40,6 +41,9 @@ function( message, target ) { | |
// Validate current page inputs | |
$("[name^='input_']:not([type='hidden'])", $currentPage).each( function() { | |
pageValid = this.checkValidity === undefined || this.checkValidity(); | |
+ if ((validationMessage = this.data('validation-message'))) { | |
+ this.setCustomValidity(pageValid ? '' : validationMessage); | |
+ } | |
return pageValid; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment