Skip to content

Commit

Permalink
ENH Used PhoneField for PhoneLink and add RequireFields validator
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Sep 26, 2023
1 parent 2c36b86 commit ebf526b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Models/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace SilverStripe\LinkField\Models;

use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\FieldList;
use SilverStripe\LinkField\Form\PhoneField;

/**
* A link to a phone number
*
Expand All @@ -15,6 +20,14 @@ class PhoneLink extends Link
'Phone' => 'Varchar(255)',
];

public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(static function (FieldList $fields) {
$fields->replaceField('Phone', PhoneField::create('Phone'));
});
return parent::getCMSFields();
}

public function generateLinkDescription(array $data): string
{
return isset($data['Phone']) ? $data['Phone'] : '';
Expand All @@ -24,4 +37,11 @@ public function getURL(): string
{
return $this->Phone ? sprintf('tel:%s', $this->Phone) : '';
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['Phone']));
return $validator;
}
}

0 comments on commit ebf526b

Please sign in to comment.