|
14 | 14 | * without interfering with the Phone field's default UX. |
15 | 15 | */ |
16 | 16 | 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 ); |
24 | 26 | } |
25 | 27 | 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 ); |
38 | 29 |
|
39 | 30 | /** |
40 | 31 | * Remove the format-specific validation message that is added by Gravity Forms when a Phone field is marked as having |
41 | 32 | * failed validation. See the more detailed comment above the setting of the `origPhoneFormat` property above. |
42 | 33 | */ |
43 | 34 | add_filter( 'gform_field_content', function( $content, $field ) { |
44 | 35 |
|
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 ) { |
46 | 37 | return $content; |
47 | 38 | } |
48 | 39 |
|
|
0 commit comments