Skip to content

Commit

Permalink
Fix bug causing form to not submit on empty US phone numbers
Browse files Browse the repository at this point in the history
The form must return null when a field is not filled out. Other values will cause a validation failure
  • Loading branch information
MaxwellGarceau committed Jan 8, 2025
1 parent 0348809 commit 37ffabf
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions includes/validation/class-validate-merge-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,30 @@ public function __construct( $wp_error_factory = null ) {
/**
* Validate phone
*
* Filters and formats the phone number for validation. Returns:
* - `string` on successful validation.
* - `null` if the field is optional and not filled out.
* - `WP_Error` on validation failure.
*
* @param array $opt_val Option value
* @param array $data Data
* @return array|WP_Error
* @return string|null|WP_Error
*/
public function validate_phone( $opt_val, $data ) {
// Filter out falsy values
$opt_val = array_filter( $opt_val );

// If they were all empty
/**
* Conditions
* - Field is not filled out - skip validation
* - Field is empty after filtering falsy values
*
* MUST return null if field is optional and not filled out
* Any other value will cause a validation error that prevents form
* from submitting
*/
if ( ! $opt_val ) {
return $opt_val;
return null;
}

// Trim Whitespace - Beginning and end
Expand Down

0 comments on commit 37ffabf

Please sign in to comment.