-
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 21aea06
Showing
8 changed files
with
120 additions
and
6 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
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,55 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Form; | ||
|
||
use SilverStripe\Forms\TextField; | ||
use SilverStripe\Forms\Validator; | ||
|
||
/** | ||
* Text input field with validation for a url | ||
* Only intended to be used with the ExternalLink DataObject | ||
*/ | ||
class ExternalLinkField extends TextField | ||
{ | ||
/** | ||
* This needs to be not surronded by regex delimiters so that it works on the frontend | ||
* https://urlregex.com/ | ||
*/ | ||
private const JS_RX = '((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)'; | ||
|
||
/** | ||
* https://urlregex.com/ | ||
*/ | ||
private const PHP_RX = '%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu'; | ||
|
||
/** | ||
* @param Validator $validator | ||
* | ||
* @return string | ||
*/ | ||
public function validate($validator) | ||
{ | ||
$result = true; | ||
$this->value = trim($this->value ?? ''); | ||
if ($this->value && !preg_match(self::PHP_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::JS_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