From 45c544c801550b5453f23bc6af429c234bd1fa87 Mon Sep 17 00:00:00 2001 From: iainsaxon Date: Thu, 7 Oct 2021 12:30:39 +1100 Subject: [PATCH] Phone number and email validation improvements --- CHANGELOG.md | 6 ++++ composer.json | 2 +- src/fields/Email.php | 8 +++++ src/fields/Telephone.php | 30 ++++++++++++------- .../fieldtypes/Telephone/input.twig | 2 +- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f6e46f..c0da0a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 1.0.3 - 2021.10.07 +### Changed +- Phone number country code select input no longer has an empty value and will default to the field's Default Country Code setting. #63 - https://github.com/newism/craft3-fields/issues/63 +- Phone number validation message will point out that the phone number could be incorrect for the selected country code +- Email address validator adds a regex validation to find characters that would prevent a valid email and has an error message to reflect this + ## 1.0.2 - 2021.01.19 ### Fixes - MapURL is persisted. #61 - https://github.com/newism/craft3-fields/pull/61 @thomasgoemaere diff --git a/composer.json b/composer.json index 57d03fa..5c352c9 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "newism/craft3-fields", "description": "Address, telephone and email fields for CraftCMS 3.x", "type": "craft-plugin", - "version": "1.0.2", + "version": "1.0.3", "keywords": [ "craft", "cms", diff --git a/src/fields/Email.php b/src/fields/Email.php index c8cdd06..30d1e1c 100644 --- a/src/fields/Email.php +++ b/src/fields/Email.php @@ -55,6 +55,14 @@ public function getSettingsHtml() public function getElementValidationRules(): array { $rules = parent::getElementValidationRules(); + $rules[] = 'trim'; + $rules[] = [ + // the value is valid if it doesn't find a non-basic character + 'match', + 'not' => true, + 'pattern' => '/[^a-z0-9_\-\@+\.]/i', + 'message' => 'Some invalid or hidden characters were detected in the address. Please retype or paste as plain text' + ]; $rules[] = 'email'; return $rules; diff --git a/src/fields/Telephone.php b/src/fields/Telephone.php index 7b1049c..4ff074c 100644 --- a/src/fields/Telephone.php +++ b/src/fields/Telephone.php @@ -110,24 +110,29 @@ public function rules(): array */ public function normalizeValue($value, ElementInterface $element = null) { + /** + * Just return value if it's already an TelephoneModel. + */ + if ($value instanceof TelephoneModel) { + return $value; + } /** * Serialised value from the DB */ if (is_string($value)) { $value = json_decode($value, true); } - /** - * Default values + * Array value from post or unserialized array */ - if (!is_array($value)) { - $value = [ - 'countryCode' => $this->defaultCountryCode, - 'rawInput' => '', - ]; + if (is_array($value) && !empty(array_filter($value))) { + return new TelephoneModel( + strlen($value['countryCode']) ? $value['countryCode'] : $this->defaultCountryCode, + $value['rawInput'] + ); } - return new TelephoneModel($value['countryCode'] ?? $this->defaultCountryCode, $value['rawInput']); + return null; } /** @@ -202,7 +207,10 @@ public function getInputHtml( */ private function getCountryOptions(): array { - $countries = [['value' => '', 'label' => '']]; + // Removing the null default value as this causes more problems + // We already have a default country code defined in the settings and trying to manage a field that can have + // the first Telephone object as an optional parameter is difficult. +// $countries = [['value' => '', 'label' => '']]; $countryRepository = new CountryRepository(); $countryData = $countryRepository->getList(); @@ -262,7 +270,7 @@ public function getElementValidationRules(): array public function isValueEmpty($value, ElementInterface $element = null): bool { if ($value instanceof TelephoneModel) { - return (null === $value->phoneNumber); + return 0 === strlen($value->phoneNumber); } return parent::isValueEmpty($value, $element); @@ -291,7 +299,7 @@ public function validatePhoneNumber( $this->handle, Craft::t( 'nsm-fields', - 'The string supplied did not seem to be a phone number.' + 'The string supplied did not seem to be a phone number or didn\'t match the expected format for the country.' ) ); } diff --git a/src/templates/_components/fieldtypes/Telephone/input.twig b/src/templates/_components/fieldtypes/Telephone/input.twig index f62f1fb..3008339 100644 --- a/src/templates/_components/fieldtypes/Telephone/input.twig +++ b/src/templates/_components/fieldtypes/Telephone/input.twig @@ -21,7 +21,7 @@ label: 'Country Code', id: name ~ 'CountryCode', name: name ~ '[countryCode]', - value: viewData ? viewData.countryCode, + value: viewData ? viewData.countryCode : settings.defaultCountryCode, options: countryOptions }) }}