Skip to content

Commit

Permalink
gpapf-validation-only.php: Fixed issue where validation was not cor…
Browse files Browse the repository at this point in the history
…rectly applied.
  • Loading branch information
spivurno committed Oct 11, 2023
1 parent 0e5d365 commit 7c6308b
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions gp-advanced-phone-field/gpapf-validation-only.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,26 @@
* without interfering with the Phone field's default UX.

Check failure on line 14 in gp-advanced-phone-field/gpapf-validation-only.php

View workflow job for this annotation

GitHub Actions / PHPCS

Tabs must be used to indent lines; spaces are not allowed
*/
add_filter( 'gform_field_validation', function( $result, $value, $form, $field ) {
if ( $field->get_input_type() === 'phone' && $field->phoneFormat === 'standard' && ! $field->gpapfEnable ) {
$field->gpapfEnable = true;
// GPAPF unsets the `phoneFormat` to avoid format-specific validation messages. We need to store the original
// format and reset it after GPAFP's validation. Unfortunately, since we need GF to initialize its input mask
// based on the `phoneFormat`, we have to allow it to output its format-specific validation message. Which we
// remove via DOM manipulation below.
$field->origPhoneFormat = $field->phoneFormat;
if ( $field->get_input_type() === 'phone'
&& $field->phoneFormat === 'standard'

Check warning on line 18 in gp-advanced-phone-field/gpapf-validation-only.php

View workflow job for this annotation

GitHub Actions / PHPCS

Found precision alignment of 1 spaces.

Check failure on line 18 in gp-advanced-phone-field/gpapf-validation-only.php

View workflow job for this annotation

GitHub Actions / PHPCS

Tabs must be used to indent lines; spaces are not allowed
&& ! $field->gpapfEnable

Check warning on line 19 in gp-advanced-phone-field/gpapf-validation-only.php

View workflow job for this annotation

GitHub Actions / PHPCS

Found precision alignment of 1 spaces.

Check failure on line 19 in gp-advanced-phone-field/gpapf-validation-only.php

View workflow job for this annotation

GitHub Actions / PHPCS

Tabs must be used to indent lines; spaces are not allowed
&& ! rgblank( $value )

Check warning on line 20 in gp-advanced-phone-field/gpapf-validation-only.php

View workflow job for this annotation

GitHub Actions / PHPCS

Found precision alignment of 1 spaces.
) {
$cloned_field = clone $field;
$cloned_field->gpapfEnable = true;
$parsed_phone_number = '+1' . preg_replace( '/[^0-9]/', '', $value );
$result = gp_advanced_phone_field()->validation( $result, $parsed_phone_number, $form, $cloned_field );
}
return $result;
}, 9, 4 );

/**
* Disable GPAPF after validation so that it does not interfere with the default Phone field's input mask.
*/
add_filter( 'gform_field_validation', function( $result, $value, $form, $field ) {
if ( $field->origPhoneFormat ) {
$field->gpapfEnable = false;
$field->phoneFormat = $field->origPhoneFormat;
}
return $result;
}, 11, 4 );
}, 10, 4 );

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

if ( $field->get_input_type() !== 'phone' || $field->phoneFormat !== 'standard' || $field->gpapfEnable ) {
if ( $field->is_form_editor() || $field->get_input_type() !== 'phone' || $field->phoneFormat !== 'standard' || $field->gpapfEnable ) {
return $content;
}

Expand Down

0 comments on commit 7c6308b

Please sign in to comment.