Skip to content

Commit 86361cc

Browse files
committed
v1.4.0
1 parent dfeb663 commit 86361cc

3 files changed

Lines changed: 107 additions & 41 deletions

File tree

README.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Donate link: https://aitorres.com/
44
Tags: elementor, elementor-forms, validation
55
Requires at least: 3.0.1
66
Tested up to: 6.8
7-
Stable tag: 1.3.0
7+
Stable tag: 1.4.0
88
License: GPLv2 or later
99
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -35,6 +35,8 @@ Supported actions include:
3535

3636
Validations set for optional fields will only run if the field is not empty.
3737

38+
Custom validation messages can also be configured for each action independently.
39+
3840

3941
== Installation ==
4042

@@ -72,6 +74,9 @@ No! You can specify validation actions for just one field, or as many as you'd l
7274

7375
== Changelog ==
7476

77+
= 1.4.0 =
78+
* Introduce a new field on each validation action to optionally set up a custom validation message.
79+
7580
= 1.3.0 =
7681
* Introduced a new validation action: matches a regular expression.
7782
* Tested against WordPress 6.8 and the latest Elementor version.

actions/class-ait-validation-action.php

Lines changed: 98 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public function run( $record, $ajax_handler ) {
7979
$validation_action = $item['validation_action'];
8080
$validation_field_id = $item['validation_field_id'];
8181
$validation_value = $item['validation_value'];
82+
$custom_error_message = isset($item['custom_error_message']) && !empty($item['custom_error_message'])
83+
? $item['custom_error_message']
84+
: '';
8285

8386
// Checking if the field exists in the form
8487
if ( ! isset( $fields[ $validation_field_id ] ) ) {
@@ -92,64 +95,78 @@ public function run( $record, $ajax_handler ) {
9295
switch ( $validation_action ) {
9396
case Ait_Validation_Action_Elementor_Forms_Constants::ACTION_MIN_LENGTH:
9497
if ( strlen( $field_value ) < $validation_value ) {
95-
$ajax_handler->add_error(
98+
$this->add_validation_error(
99+
$ajax_handler,
96100
$validation_field_id,
97-
/* translators: %d: minimum length */
98-
sprintf( esc_html__( 'Field must be at least %d characters long.', 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
101+
$custom_error_message,
102+
'Field must be at least %d characters long.',
103+
$validation_value
99104
);
100105
}
101106
break;
102107
case Ait_Validation_Action_Elementor_Forms_Constants::ACTION_MAX_LENGTH:
103108
if ( strlen( $field_value ) > $validation_value ) {
104-
$ajax_handler->add_error(
109+
$this->add_validation_error(
110+
$ajax_handler,
105111
$validation_field_id,
106-
/* translators: %d: maximum length */
107-
sprintf( esc_html__( 'Field must be at most %d characters long.', 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
112+
$custom_error_message,
113+
'Field must be at most %d characters long.',
114+
$validation_value
108115
);
109116
}
110117
break;
111118
case Ait_Validation_Action_Elementor_Forms_Constants::ACTION_MIN_WORDS:
112119
if ( str_word_count( $field_value ) < $validation_value ) {
113-
$ajax_handler->add_error(
120+
$this->add_validation_error(
121+
$ajax_handler,
114122
$validation_field_id,
115-
/* translators: %d: minimum words */
116-
sprintf( esc_html__( 'Field must have at least %d words.', 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
123+
$custom_error_message,
124+
'Field must have at least %d words.',
125+
$validation_value
117126
);
118127
}
119128
break;
120129
case Ait_Validation_Action_Elementor_Forms_Constants::ACTION_MAX_WORDS:
121130
if ( str_word_count( $field_value ) > $validation_value ) {
122-
$ajax_handler->add_error(
131+
$this->add_validation_error(
132+
$ajax_handler,
123133
$validation_field_id,
124-
/* translators: %d: maximum words */
125-
sprintf( esc_html__( 'Field must have at most %d words.', 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
134+
$custom_error_message,
135+
'Field must have at most %d words.',
136+
$validation_value
126137
);
127138
}
128139
break;
129140
case Ait_Validation_Action_Elementor_Forms_Constants::ACTION_STARTS_WITH:
130141
if ( strpos( $field_value, $validation_value ) !== 0 ) {
131-
$ajax_handler->add_error(
142+
$this->add_validation_error(
143+
$ajax_handler,
132144
$validation_field_id,
133-
/* translators: %s: validation value the field must start with */
134-
sprintf( esc_html__( 'Field must start with %s.', 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
145+
$custom_error_message,
146+
'Field must start with %s.',
147+
$validation_value
135148
);
136149
}
137150
break;
138151
case Ait_Validation_Action_Elementor_Forms_Constants::ACTION_ENDS_WITH:
139152
if ( substr( $field_value, -strlen( $validation_value ) ) !== $validation_value ) {
140-
$ajax_handler->add_error(
153+
$this->add_validation_error(
154+
$ajax_handler,
141155
$validation_field_id,
142-
/* translators: %s: validation value the field must end with */
143-
sprintf( esc_html__( 'Field must end with %s.', 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
156+
$custom_error_message,
157+
'Field must end with %s.',
158+
$validation_value
144159
);
145160
}
146161
break;
147162
case Ait_Validation_Action_Elementor_Forms_Constants::ACTION_CONTAINS:
148163
if ( strpos( $field_value, $validation_value ) === false ) {
149-
$ajax_handler->add_error(
164+
$this->add_validation_error(
165+
$ajax_handler,
150166
$validation_field_id,
151-
/* translators: %s: validation value the field must contain */
152-
sprintf( esc_html__( 'Field must contain %s.', 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
167+
$custom_error_message,
168+
'Field must contain %s.',
169+
$validation_value
153170
);
154171
}
155172
break;
@@ -163,27 +180,33 @@ public function run( $record, $ajax_handler ) {
163180
$field_to_match = $fields[ $validation_value ];
164181

165182
if ( $field_value !== $field_to_match ) {
166-
$ajax_handler->add_error(
183+
$this->add_validation_error(
184+
$ajax_handler,
167185
$validation_field_id,
168-
esc_html__( 'Field does not match the required field.', 'ait-form-validation-action-addon-for-elementor' )
186+
$custom_error_message,
187+
'Field does not match the required field.'
169188
);
170189
}
171190
break;
172191
case Ait_Validation_Action_Elementor_Forms_Constants::ACTION_AFTER_THAN:
173192
if ( strtotime( $field_value ) <= strtotime( $validation_value ) ) {
174-
$ajax_handler->add_error(
193+
$this->add_validation_error(
194+
$ajax_handler,
175195
$validation_field_id,
176-
/* translators: %s: validation value the field must be after */
177-
sprintf( esc_html__( 'Field must be after %s.', 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
196+
$custom_error_message,
197+
'Field must be after %s.',
198+
$validation_value
178199
);
179200
}
180201
break;
181202
case Ait_Validation_Action_Elementor_Forms_Constants::ACTION_BEFORE_THAN:
182203
if ( strtotime( $field_value ) >= strtotime( $validation_value ) ) {
183-
$ajax_handler->add_error(
204+
$this->add_validation_error(
205+
$ajax_handler,
184206
$validation_field_id,
185-
/* translators: %s: validation value the field must be before */
186-
sprintf( esc_html__( 'Field must be before %s.', 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
207+
$custom_error_message,
208+
'Field must be before %s.',
209+
$validation_value
187210
);
188211
}
189212
break;
@@ -197,9 +220,11 @@ public function run( $record, $ajax_handler ) {
197220
$field_to_match = $fields[ $validation_value ];
198221

199222
if ( strtotime( $field_value ) <= strtotime( $field_to_match ) ) {
200-
$ajax_handler->add_error(
223+
$this->add_validation_error(
224+
$ajax_handler,
201225
$validation_field_id,
202-
esc_html__( 'Field must be after the required field.', 'ait-form-validation-action-addon-for-elementor' )
226+
$custom_error_message,
227+
'Field must be after the required field.'
203228
);
204229
}
205230
break;
@@ -213,18 +238,22 @@ public function run( $record, $ajax_handler ) {
213238
$field_to_match = $fields[ $validation_value ];
214239

215240
if ( strtotime( $field_value ) >= strtotime( $field_to_match ) ) {
216-
$ajax_handler->add_error(
241+
$this->add_validation_error(
242+
$ajax_handler,
217243
$validation_field_id,
218-
esc_html__( 'Field must be before the required field.', 'ait-form-validation-action-addon-for-elementor' )
244+
$custom_error_message,
245+
'Field must be before the required field.'
219246
);
220247
}
221248
break;
222249
case Ait_Validation_Action_Elementor_Forms_Constants::ACTION_MATCHES_REGEX:
223250
if ( ! preg_match( $validation_value, $field_value ) ) {
224-
$ajax_handler->add_error(
251+
$this->add_validation_error(
252+
$ajax_handler,
225253
$validation_field_id,
226-
/* translators: %s: validation value the field must match */
227-
sprintf( esc_html__( 'Field must match the required pattern %s.', 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
254+
$custom_error_message,
255+
'Field must match the required pattern %s.',
256+
$validation_value
228257
);
229258
}
230259
break;
@@ -233,6 +262,31 @@ public function run( $record, $ajax_handler ) {
233262

234263
}
235264

265+
/**
266+
* Add validation error with custom or default message.
267+
*
268+
* @since 1.0.0
269+
* @access private
270+
* @param \ElementorPro\Modules\Forms\Classes\Ajax_Handler $ajax_handler
271+
* @param string $field_id
272+
* @param string $custom_message
273+
* @param string $default_message
274+
* @param mixed $validation_value
275+
*/
276+
private function add_validation_error( $ajax_handler, $field_id, $custom_message, $default_message, $validation_value = null ) {
277+
if ( !empty( $custom_message ) ) {
278+
$error_message = $validation_value !== null
279+
? esc_html( sprintf( $custom_message, $validation_value ) )
280+
: esc_html( $custom_message );
281+
} else {
282+
$error_message = $validation_value !== null
283+
? sprintf( esc_html__( $default_message, 'ait-form-validation-action-addon-for-elementor' ), $validation_value )
284+
: esc_html__( $default_message, 'ait-form-validation-action-addon-for-elementor' );
285+
}
286+
287+
$ajax_handler->add_error( $field_id, $error_message );
288+
}
289+
236290
/**
237291
* Register action controls.
238292
*
@@ -296,6 +350,13 @@ public function register_settings_section( $widget ) {
296350
'label' => esc_html__( 'Validation Value', 'ait-form-validation-action-addon-for-elementor' ),
297351
'type' => \Elementor\Controls_Manager::TEXT,
298352
'default' => '',
353+
],
354+
[
355+
'name' => 'custom_error_message',
356+
'description' => esc_html__( 'Optional custom error message. Use %s as placeholder for the validation value. Leave blank to use the default.', 'ait-form-validation-action-addon-for-elementor' ),
357+
'label' => esc_html__( 'Custom Error Message', 'ait-form-validation-action-addon-for-elementor' ),
358+
'type' => \Elementor\Controls_Manager::TEXT,
359+
'default' => '',
299360
]
300361
],
301362
'default' => [],
@@ -318,4 +379,4 @@ public function register_settings_section( $widget ) {
318379
*/
319380
public function on_export( $element ) {}
320381

321-
}
382+
}

ait-form-validation-action-addon-for-elementor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Plugin Name: Form Validation Action Addon for Elementor
1717
* Plugin URI: https://aitorres.com/projects/ait-form-validation-action-addon-for-elementor/
1818
* Description: Add a custom validation action for Forms to your Elementor installation to run server-side validation for your form fields.
19-
* Version: 1.3.0
19+
* Version: 1.4.0
2020
* Author: Andrés Ignacio Torres
2121
* Author URI: https://aitorres.com/
2222
* License: GPL-2.0+
@@ -25,8 +25,8 @@
2525
* Domain Path: /languages
2626
*
2727
* Requires Plugins: elementor
28-
* Elementor tested up to: 3.28.3
29-
* Elementor Pro tested up to: 3.28.1
28+
* Elementor tested up to: 3.30.1
29+
* Elementor Pro tested up to: 3.30.1
3030
*/
3131

3232
// If this file is called directly, abort.

0 commit comments

Comments
 (0)