-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a46c745
commit d19ee2e
Showing
12 changed files
with
304 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Form; | ||
|
||
use SilverStripe\Forms\TextField; | ||
use SilverStripe\Forms\Validator; | ||
|
||
/** | ||
* Text input field with validation for a url | ||
*/ | ||
class ExternalLinkField extends TextField | ||
{ | ||
/** | ||
* This needs to be not surronded by regex delimiters so that it works on the frontend | ||
*/ | ||
private const RX = '^[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$'; | ||
|
||
/** | ||
* This is used for the <input> element type="text" attribute | ||
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/tel | ||
*/ | ||
protected $inputType = 'url'; | ||
|
||
/** | ||
* This is added as a classname to the <input> element | ||
*/ | ||
public function Type() | ||
{ | ||
return 'url text'; | ||
} | ||
|
||
/** | ||
* @param Validator $validator | ||
* | ||
* @return string | ||
*/ | ||
public function validate($validator) | ||
{ | ||
$result = true; | ||
$this->value = trim($this->value ?? ''); | ||
if ($this->value && !preg_match('#' . self::RX . '#', $this->value)) { | ||
$validator->validationError( | ||
$this->name, | ||
_t(__CLASS__ . '.INVALID', 'Please enter a valid url'), | ||
'validation' | ||
); | ||
$result = false; | ||
} | ||
return $this->extendValidationResult($result, $validator); | ||
} | ||
|
||
/** | ||
* This is passed to the frontent via FormField::getSchemaValidation() | ||
* and used in Validator.js | ||
*/ | ||
public function getSchemaValidation() | ||
{ | ||
$rules = parent::getSchemaValidation(); | ||
$rules['regex'] = ['pattern' => self::RX]; | ||
return $rules; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Form; | ||
|
||
use LogicException; | ||
use SilverStripe\Admin\Forms\LinkFormFactory; | ||
use SilverStripe\Forms\HiddenField; | ||
use SilverStripe\LinkField\Type\Type; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\Core\Injector\Injector; | ||
|
||
/** | ||
* Create Form schema for the LinkField based on a key provided by the request. | ||
*/ | ||
class FormFactory extends LinkFormFactory | ||
{ | ||
protected function getFormFields($controller, $name, $context) | ||
{ | ||
/** @var Type $type */ | ||
$type = $context['LinkType']; | ||
|
||
if (!$type instanceof Type) { | ||
throw new LogicException(sprintf('%s: LinkType must be provided and must be an instance of Type', static::class)); | ||
} | ||
|
||
// Pass on any available link data | ||
$linkData = array_key_exists('LinkData', $context) | ||
? $context['LinkData'] | ||
: []; | ||
$fields = $type->scaffoldLinkFields($linkData); | ||
$fields->push(HiddenField::create('typeKey')->setValue($context['LinkTypeKey'])); | ||
$this->extend('updateFormFields', $fields, $controller, $name, $context); | ||
|
||
return $fields; | ||
} | ||
|
||
protected function getValidator($controller, $name, $context) | ||
{ | ||
if (!array_key_exists('LinkType', $context)) { | ||
return null; | ||
} | ||
/** @var DataObject|Type $type */ | ||
$type = $context['LinkType']; | ||
return Injector::inst()->get($type)->getCMSCompositeValidator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Form; | ||
|
||
use SilverStripe\Forms\TreeDropdownField; | ||
|
||
/** | ||
* This class exists as workaround to an issue where RequiredFields is not respected | ||
* when selecting an empty value using TreeDropdownField because RequiredFields views | ||
* a value of 0 (ie no relation selected) as as non-emtpy value, as RequiredFields only counts | ||
* an emtpy string as a missing value | ||
* | ||
* This class in only intended to be used in SiteTreeLink which also has a RequiredFields of ['PageID'] | ||
*/ | ||
class LinkFieldTreeDropdownField extends TreeDropdownField | ||
{ | ||
/** | ||
* This is passed to the frontent via FormField::getSchemaValidation() | ||
* and used in Validator.js | ||
*/ | ||
public function getSchemaValidation() | ||
{ | ||
$rules = parent::getSchemaValidation(); | ||
$rules['regex'] = ['pattern' => '[^0]']; | ||
return $rules; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Form; | ||
|
||
use SilverStripe\Forms\TextField; | ||
use SilverStripe\Forms\Validator; | ||
|
||
/** | ||
* Text input field with validation for a phone number | ||
*/ | ||
class PhoneField extends TextField | ||
{ | ||
/** | ||
* This needs to be not surronded by regex delimiters so that it works on the frontend | ||
*/ | ||
private const RX = '^[()\- 0-9]+$'; | ||
|
||
/** | ||
* This is used for the <input> element type="tel" attribute | ||
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/tel | ||
*/ | ||
protected $inputType = 'tel'; | ||
|
||
/** | ||
* This is added as a classname to the <input> element | ||
*/ | ||
public function Type() | ||
{ | ||
return 'phone text'; | ||
} | ||
|
||
/** | ||
* @param Validator $validator | ||
* | ||
* @return string | ||
*/ | ||
public function validate($validator) | ||
{ | ||
$result = true; | ||
$this->value = trim($this->value ?? ''); | ||
if ($this->value && !preg_match('#' . self::RX . '#', $this->value)) { | ||
$validator->validationError( | ||
$this->name, | ||
_t(__CLASS__ . '.INVALID', 'Please enter a valid phone number'), | ||
'validation' | ||
); | ||
$result = false; | ||
} | ||
return $this->extendValidationResult($result, $validator); | ||
} | ||
|
||
/** | ||
* This is passed to the frontent via FormField::getSchemaValidation() | ||
* and used in Validator.js | ||
*/ | ||
public function getSchemaValidation() | ||
{ | ||
$rules = parent::getSchemaValidation(); | ||
$rules['regex'] = ['pattern' => self::RX]; | ||
return $rules; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.