Skip to content

Commit 7c6308b

Browse files
committed
gpapf-validation-only.php: Fixed issue where validation was not correctly applied.
1 parent 0e5d365 commit 7c6308b

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

gp-advanced-phone-field/gpapf-validation-only.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,26 @@
1414
* without interfering with the Phone field's default UX.
1515
*/
1616
add_filter( 'gform_field_validation', function( $result, $value, $form, $field ) {
17-
if ( $field->get_input_type() === 'phone' && $field->phoneFormat === 'standard' && ! $field->gpapfEnable ) {
18-
$field->gpapfEnable = true;
19-
// GPAPF unsets the `phoneFormat` to avoid format-specific validation messages. We need to store the original
20-
// format and reset it after GPAFP's validation. Unfortunately, since we need GF to initialize its input mask
21-
// based on the `phoneFormat`, we have to allow it to output its format-specific validation message. Which we
22-
// remove via DOM manipulation below.
23-
$field->origPhoneFormat = $field->phoneFormat;
17+
if ( $field->get_input_type() === 'phone'
18+
&& $field->phoneFormat === 'standard'
19+
&& ! $field->gpapfEnable
20+
&& ! rgblank( $value )
21+
) {
22+
$cloned_field = clone $field;
23+
$cloned_field->gpapfEnable = true;
24+
$parsed_phone_number = '+1' . preg_replace( '/[^0-9]/', '', $value );
25+
$result = gp_advanced_phone_field()->validation( $result, $parsed_phone_number, $form, $cloned_field );
2426
}
2527
return $result;
26-
}, 9, 4 );
27-
28-
/**
29-
* Disable GPAPF after validation so that it does not interfere with the default Phone field's input mask.
30-
*/
31-
add_filter( 'gform_field_validation', function( $result, $value, $form, $field ) {
32-
if ( $field->origPhoneFormat ) {
33-
$field->gpapfEnable = false;
34-
$field->phoneFormat = $field->origPhoneFormat;
35-
}
36-
return $result;
37-
}, 11, 4 );
28+
}, 10, 4 );
3829

3930
/**
4031
* Remove the format-specific validation message that is added by Gravity Forms when a Phone field is marked as having
4132
* failed validation. See the more detailed comment above the setting of the `origPhoneFormat` property above.
4233
*/
4334
add_filter( 'gform_field_content', function( $content, $field ) {
4435

45-
if ( $field->get_input_type() !== 'phone' || $field->phoneFormat !== 'standard' || $field->gpapfEnable ) {
36+
if ( $field->is_form_editor() || $field->get_input_type() !== 'phone' || $field->phoneFormat !== 'standard' || $field->gpapfEnable ) {
4637
return $content;
4738
}
4839

0 commit comments

Comments
 (0)