-
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
7a16b8b
commit f78ecb4
Showing
12 changed files
with
261 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,7 @@ | ||
en: | ||
SilverStripe\LinkField\Form\ExternalLinkField: | ||
INVALID: 'Please enter a valid url' | ||
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,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,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