Skip to content

Commit

Permalink
ENH Improved validation
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 30, 2024
1 parent a46c745 commit 7752fc7
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ class MyCustomLink extends Link
}
```

## Custom link validation

Custom links can have validation set using standard [model validation]([https://docs.silverstripe.org/en/5/developer_guides/forms/validation/#model-validation).

## Migrating from Shae Dawson's Linkable module

https://github.com/sheadawson/silverstripe-linkable
Expand Down
7 changes: 5 additions & 2 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ en:
CREATE_LINK: 'Create link'
MENUTITLE: 'Link fields'
UPDATE_LINK: 'Update link'
SilverStripe\LinkField\Form\ExternalLinkField:
INVALID: 'Please enter a valid URL'
SilverStripe\LinkField\Form\Traits\AllowedLinkClassesTrait:
INVALID_TYPECLASS: '"{class}": {typeclass} is not a valid Link Type'
INVALID_TYPECLASS_EMPTY: '"{class}": Allowed types cannot be empty'
Expand All @@ -16,14 +18,15 @@ en:
SINGULARNAME: 'Email Link'
db_Email: Email
SilverStripe\LinkField\Models\ExternalLink:
EXTERNAL_URL_FIELD: 'External url'
EXTERNAL_URL_FIELD: 'External URL'
EXTERNAL_URL_FIELD_DESCRIPTION: 'Ensure the URL starts with http:// or https://'
LINKLABEL: 'Link to external URL'
PLURALNAME: 'External Links'
PLURALS:
one: 'An External Link'
other: '{count} External Links'
SINGULARNAME: 'External Link'
db_ExternalUrl: 'External url'
db_ExternalUrl: 'External URL'
SilverStripe\LinkField\Models\FileLink:
CANNOT_VIEW_FILE: 'Cannot view file'
FILE_DOES_NOT_EXIST: 'File does not exist'
Expand Down
9 changes: 9 additions & 0 deletions src/Models/EmailLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use SilverStripe\Forms\EmailField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\RequiredFields;

/**
* A link to an Email address.
Expand Down Expand Up @@ -55,4 +57,11 @@ public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to email address');
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['Email']));
return $validator;
}
}
19 changes: 17 additions & 2 deletions src/Models/ExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace SilverStripe\LinkField\Models;

use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\UrlField;

/**
* A link to an external URL.
Expand All @@ -27,8 +30,13 @@ class ExternalLink extends Link
public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$linkField = $fields->dataFieldByName('ExternalUrl');
$linkField->setTitle(_t(__CLASS__ . '.EXTERNAL_URL_FIELD', 'External url'));
$field = UrlField::create('ExternalUrl');
$field->setTitle(_t(__CLASS__ . '.EXTERNAL_URL_FIELD', 'External URL'));
$field->setDescription(_t(
__CLASS__ . '.EXTERNAL_URL_FIELD_DESCRIPTION',
'Ensure the URL starts with http:// or https://'
));
$fields->replaceField('ExternalUrl', $field);
});
return parent::getCMSFields();
}
Expand All @@ -51,4 +59,11 @@ public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to external URL');
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['ExternalUrl']));
return $validator;
}
}
9 changes: 9 additions & 0 deletions src/Models/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use SilverStripe\Assets\File;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\RequiredFields;

/**
* A link to a File in the CMS
Expand Down Expand Up @@ -71,4 +73,11 @@ public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Link to a file');
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['File']));
return $validator;
}
}
14 changes: 12 additions & 2 deletions src/Models/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace SilverStripe\LinkField\Models;

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

/**
* A link to a phone number
Expand All @@ -25,8 +28,8 @@ class PhoneLink extends Link
public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$linkField = $fields->dataFieldByName('Phone');
$linkField->setTitle(_t(__CLASS__ . '.PHONE_FIELD', 'Phone'));
$field = $fields->dataFieldByName('Phone');
$field->setTitle(_t(__CLASS__ . '.PHONE_FIELD', 'Phone'));
$fields->removeByName('OpenInNew');
});
return parent::getCMSFields();
Expand All @@ -50,4 +53,11 @@ public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Phone number');
}

public function getCMSCompositeValidator(): CompositeValidator
{
$validator = parent::getCMSCompositeValidator();
$validator->addValidator(RequiredFields::create(['Phone']));
return $validator;
}
}
9 changes: 9 additions & 0 deletions src/Models/SiteTreeLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Forms\CompositeValidator;
use SilverStripe\Forms\RequiredFields;

/**
* A link to a Page in the CMS
Expand Down Expand Up @@ -147,4 +149,11 @@ public function getMenuTitle(): string
{
return _t(__CLASS__ . '.LINKLABEL', 'Page on this site');
}

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

0 comments on commit 7752fc7

Please sign in to comment.