Skip to content

Commit

Permalink
ENH Improved validation
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Feb 6, 2024
1 parent 03ffff8 commit 493a294
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 9 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
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions client/src/containers/LinkModalContainer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-disable */
import React from 'react';
import React, { useState } from 'react';
import { loadComponent } from 'lib/Injector';
import PropTypes from 'prop-types';

/**
* Contains the LinkModal and determines which modal component to render based on the link type.
*/
const LinkModalContainer = ({ types, typeKey, linkID = 0, isOpen, onSuccess, onClosed }) => {
const [LinkModal, setLinkModal] = useState(null);

if (!typeKey) {
return false;
}
Expand All @@ -15,7 +17,13 @@ const LinkModalContainer = ({ types, typeKey, linkID = 0, isOpen, onSuccess, onC
const handlerName = type && type.hasOwnProperty('handlerName')
? type.handlerName
: 'FormBuilderModal';
const LinkModal = loadComponent(`LinkModal.${handlerName}`);

// Use state to store the component so that it's only loaded once
// Not doing this will cause the component to being reloaded on every render
// which will causes bugs with validation
if (!LinkModal) {
setLinkModal(() => loadComponent(`LinkModal.${handlerName}`));
}

return <LinkModal
typeTitle={type.title || ''}
Expand Down
5 changes: 3 additions & 2 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,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: 'The URL must start with either 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',
'The URL must start with either 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 493a294

Please sign in to comment.