Skip to content

Commit 512b405

Browse files
author
Matthew Mirus
committed
First release feature complete.
1 parent 81607dd commit 512b405

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

gravityforms-regex-validation.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/*
3+
Plugin Name: Gravity Forms Regular Expression Validation
4+
Plugin URI: https://github.com/mmirus/gravityforms-regex-validation
5+
Description: Add regular expression validation option to Gravity Forms single text input
6+
Author: Matt Mirus
7+
Author URI: https://github.com/mmirus
8+
Version: 1.0
9+
GitHub Plugin URI: https://github.com/mmirus/gravityforms-regex-validation
10+
*/
11+
12+
namespace GF_RegEx;
13+
14+
class GF_RegEx {
15+
function __construct() {
16+
// add field
17+
add_action('gform_field_advanced_settings', array($this, 'settings_fields'), 10, 2);
18+
19+
// add script
20+
add_action('gform_editor_js', array($this, 'editor_script'));
21+
22+
// add validation
23+
add_filter('gform_field_validation', array($this, 'validate'), 10, 4);
24+
}
25+
26+
// add regex validation setting fields
27+
public function settings_fields($position, $form_id) {
28+
//create settings on position 375 (right after "enable password input")
29+
if ( $position == 375 ) {
30+
?>
31+
<li class="regex_setting field_setting">
32+
<input type="checkbox" id="field_regex_validation" onclick="SetFieldProperty('regexValidation', this.checked); ToggleInputRegEx()" onkeypress="SetFieldProperty('regexValidation', this.checked); ToggleInputRegEx()" />
33+
<label for="field_regex_validation" class="inline">
34+
<?php _e("Use Regular Expression Validation", "gf_regex"); ?>
35+
</label>
36+
37+
<div id="field_regex_pattern_container" style="padding-top: 10px; display: block;">
38+
<p>
39+
<label for="field_regex_pattern" class="inline">RegEx Pattern:&nbsp;</label>
40+
<input type="text" id="field_regex_pattern" onchange="SetFieldProperty('regexPattern', this.value)" class="fieldwidth-2" placeholder="">
41+
</p>
42+
<p>
43+
<label for="field_regex_message" class="inline">Validation Message:&nbsp;</label>
44+
<input type="text" id="field_regex_message" onchange="SetFieldProperty('regexMessage', this.value)" class="fieldwidth-2" placeholder="The value provided is not valid for this field.">
45+
</p>
46+
</div>
47+
</li>
48+
<?php
49+
}
50+
}
51+
52+
// Action to inject supporting script to the form editor page
53+
public function editor_script() {
54+
?>
55+
<script type='text/javascript'>
56+
// adding setting to fields of type "text"
57+
fieldSettings.text += ", .regex_setting";
58+
59+
// binding to the load field settings event to initialize the checkbox
60+
jQuery(document).bind("gform_load_field_settings", function(event, field, form) {
61+
jQuery("#field_regex_validation").attr("checked", field["regexValidation"] == true);
62+
jQuery("#field_regex_pattern").val(field["regexPattern"]);
63+
jQuery("#field_regex_message").val(field["regexMessage"]);
64+
65+
ToggleInputRegEx(true);
66+
});
67+
68+
// show / hide regex pattern input field
69+
function ToggleInputRegEx(isInit){
70+
var speed = isInit ? "" : "slow";
71+
if(jQuery('#field_regex_validation').is(":checked")){
72+
jQuery('#field_regex_pattern_container').show(speed);
73+
}
74+
else{
75+
jQuery('#field_regex_pattern_container').hide(speed);
76+
}
77+
}
78+
</script>
79+
<?php
80+
}
81+
82+
// validate submitted data against provided regex
83+
public function validate($result, $value, $form, $field) {
84+
// if validation has passed so far, and regex validation is enabled, and a pattern was provided, and a value was provided
85+
if ($result['is_valid'] && $field['regexValidation'] && !empty($field['regexPattern']) && !empty($value)) {
86+
$regex = '/' . $field['regexPattern'] . '/';
87+
if (preg_match($regex, $value) !== 1) {
88+
$result['is_valid'] = false;
89+
90+
// use custom regex validation message if provided
91+
$result['message'] = 'The value provided is not valid for this field.';
92+
if (!empty($field['regexMessage'])) {
93+
$result['message'] = $field['regexMessage'];
94+
}
95+
}
96+
}
97+
98+
return $result;
99+
}
100+
}
101+
102+
new GF_Regex();

0 commit comments

Comments
 (0)