Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from iainsaxon/version-1
Browse files Browse the repository at this point in the history
Phone number and email validation improvements
  • Loading branch information
iainsaxon committed Oct 8, 2021
2 parents 79c693f + 45c544c commit 7a46742
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/fields/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 19 additions & 11 deletions src/fields/Telephone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.'
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/_components/fieldtypes/Telephone/input.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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
}) }}

Expand Down

0 comments on commit 7a46742

Please sign in to comment.