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 #66 from iainsaxon/fix/telephone-email-validation
Browse files Browse the repository at this point in the history
Adjusted validation for the email and telephone
  • Loading branch information
iainsaxon authored Oct 7, 2021
2 parents 4cc283c + 8df03a7 commit f040f4f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
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
11 changes: 7 additions & 4 deletions src/fields/Telephone.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function normalizeValue($value, ElementInterface $element = null)
*/
if (is_array($value) && !empty(array_filter($value))) {
return new TelephoneModel(
$value['countryCode'] ?? $this->defaultCountryCode,
strlen($value['countryCode']) ? $value['countryCode'] : $this->defaultCountryCode,
$value['rawInput']
);
}
Expand Down Expand Up @@ -209,7 +209,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 @@ -269,7 +272,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 @@ -298,7 +301,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 f040f4f

Please sign in to comment.