-
Notifications
You must be signed in to change notification settings - Fork 16
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
7a16b8b
commit 326fcd0
Showing
12 changed files
with
286 additions
and
4 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -5,5 +5,4 @@ | |
|
||
// Avoid creating global variables | ||
call_user_func(function () { | ||
|
||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
en: | ||
SilverStripe\LinkField\Form\PhoneField: | ||
INVALID: 'Please enter a valid phone number' | ||
SilverStripe\LinkField\Validators\HasOneCanViewValidator: | ||
CANNOTBEVIEWED: '{name} cannot be viewed' |
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
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,98 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Validators; | ||
|
||
use SilverStripe\Core\Config\Config; | ||
use SilverStripe\Forms\Validator; | ||
use SilverStripe\Forms\FormField; | ||
|
||
/** | ||
* Call a canView() check to validate a has_one relation | ||
*/ | ||
class HasOneCanViewValidator extends Validator | ||
{ | ||
/** | ||
* List of has_one relation fields (with 'ID' suffixed) that need to pass a canView() check | ||
*/ | ||
private array $relationFields; | ||
|
||
/** | ||
* Pass each field to be validated as a separate argument to the constructor | ||
* of this object. (an array of elements are ok). | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$relationFields = func_get_args(); | ||
if (isset($relationFields[0]) && is_array($relationFields[0])) { | ||
$relationFields = $relationFields[0]; | ||
} | ||
foreach ($relationFields as $i => $relationField) { | ||
if (!preg_match('#ID$#', $relationField)) { | ||
$relationFields[$i] .= 'ID'; | ||
} | ||
} | ||
$this->relationFields = $relationFields; | ||
} | ||
|
||
/** | ||
* Allows validation of fields via specification of a php function for | ||
* validation which is executed after the form is submitted. | ||
* | ||
* @param array $data | ||
* @return boolean | ||
*/ | ||
public function php($data) | ||
{ | ||
$valid = true; | ||
$fields = $this->form->Fields(); | ||
$dataObjectClassName = get_class($this->form->getRecord()); | ||
$hasOnes = Config::inst()->get($dataObjectClassName, 'has_one'); | ||
|
||
foreach ($this->relationFields as $fieldName) { | ||
if ($fieldName instanceof FormField) { | ||
$formField = $fieldName; | ||
$fieldName = $fieldName->getName(); | ||
} else { | ||
$formField = $fields->dataFieldByName($fieldName); | ||
} | ||
|
||
if (!$formField) { | ||
continue; | ||
} | ||
|
||
$value ??= $data[$fieldName]; | ||
if (!$value) { | ||
continue; | ||
} | ||
|
||
$relation = preg_replace('#ID$#', '', $fieldName); | ||
$relationClassName = $hasOnes[$relation]; | ||
$relationObject = $relationClassName::get()->byID($value); | ||
|
||
if ($relationObject && $relationObject->canView()) { | ||
// It's valid | ||
// Note if $relationObject is null then still fail this CanView validator the same | ||
// way so that user cannot tell if the relation exists or not | ||
continue; | ||
} | ||
|
||
$errorMessage = _t( | ||
__CLASS__ . '.CANNOTBEVIEWED', | ||
'{name} cannot be viewed', | ||
[ | ||
'name' => strip_tags( | ||
'"' . ($formField->Title() ? $formField->Title() : $fieldName) . '"' | ||
) | ||
] | ||
); | ||
if ($msg = $formField->getCustomValidationMessage()) { | ||
$errorMessage = $msg; | ||
} | ||
$this->validationError($fieldName, $errorMessage, 'required'); | ||
$valid = false; | ||
} | ||
|
||
return $valid; | ||
} | ||
} |