From 1ceefb36ef3349450aa6812e6483d3c877caff08 Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 8 Sep 2022 06:32:59 -0500 Subject: [PATCH] fix(field): Fix phone number validation (Fixes #49) (#50) * chore(field): nitpick empty country validation message --- plugin.php | 2 +- src/PhoneNumberField.php | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/plugin.php b/plugin.php index 476b4e1..06dcbfe 100644 --- a/plugin.php +++ b/plugin.php @@ -4,7 +4,7 @@ * Plugin Name: Advanced Custom Fields: Phone Number * Plugin URI: https://github.com/log1x/acf-phone-number * Description: A real ACF phone number field. - * Version: 1.1.8 + * Version: 1.1.9 * Author: Brandon Nifong * Author URI: https://github.com/log1x */ diff --git a/src/PhoneNumberField.php b/src/PhoneNumberField.php index 4131ba2..a3e31ae 100644 --- a/src/PhoneNumberField.php +++ b/src/PhoneNumberField.php @@ -152,16 +152,23 @@ public function update_value($value, $post_id, $field) */ public function validate_value($valid, $value, $field, $input) { - if (! is_array($value) || empty($value['number'])) { + if (! empty($value['number']) && ! (new PhoneNumber($value))->isValid()) { + return __('The phone number specified is not valid.', 'acf-phone-number'); + } + + if (! $field['required']) { + return $valid; + } + + if (empty($value['number'])) { return __('The phone number cannot be empty.', 'acf-phone-number'); } if (empty($value['country'])) { - return __('The phone number country specified is not valid.', 'acf-phone-number'); + return __('The phone number country cannot be empty.', 'acf-phone-number'); } - return (new PhoneNumber($value))->isValid() ? - $valid : __('The phone number specified is not valid.', 'acf-phone-number'); + return $valid; } /**