-
Notifications
You must be signed in to change notification settings - Fork 824
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
1eadc84
commit 4c975d4
Showing
2 changed files
with
171 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Forms; | ||
|
||
/** | ||
* Text input field with validation for a url | ||
* Can optionally require a scheme (e.g. https://) | ||
*/ | ||
class UrlField extends TextField | ||
{ | ||
private bool $requireScheme = false; | ||
|
||
public function getRequireScheme(): bool | ||
{ | ||
return $this->requireScheme; | ||
} | ||
|
||
public function setRequireScheme(bool $requireScheme): self | ||
{ | ||
$this->requireScheme = $requireScheme; | ||
return $this; | ||
} | ||
|
||
public function Type() | ||
{ | ||
return 'text'; | ||
} | ||
|
||
public function validate($validator) | ||
{ | ||
$result = true; | ||
$parsed = parse_url($this->value ?? ''); | ||
$value = $this->value; | ||
// filter_var() will fail underscores in domain and subdomains | ||
$value = str_replace('_', '-', $value); | ||
if (!isset($parsed['scheme']) && !$this->getRequireScheme()) { | ||
// filter_var() requires a scheme | ||
$value = 'https://' . $value; | ||
} | ||
$valid = filter_var($value, FILTER_VALIDATE_URL); | ||
if ($this->value && (!$valid || !$parsed)) { | ||
$validator->validationError( | ||
$this->name, | ||
_t(__CLASS__ . '.INVALID', 'Please enter a valid URL'), | ||
'validation' | ||
); | ||
$result = false; | ||
} | ||
return $this->extendValidationResult($result, $validator); | ||
} | ||
} |
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,120 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Forms\Tests; | ||
|
||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Forms\UrlField; | ||
use SilverStripe\Forms\RequiredFields; | ||
|
||
class UrlFieldTest extends SapphireTest | ||
{ | ||
public function provideValidate(): array | ||
{ | ||
return [ | ||
[ | ||
'url' => '', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => '', | ||
'requireScheme' => true, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'http://example-123.com', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'https://example-123.com', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'ftp://example-123.com', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'http://example-123.com:8080', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'http://example_with_underscore_in_host.com', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'http://subdomain.example-123.com', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'http://subdomain_with_underscores.example-123.com', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'http://subdomain-with-dashes.example-123.com', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'http://example-123.com:8080/path_with_underscores_(and)_parens-and-dashes', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'http://example-123.com:8080/path/?query=string&some=1#fragment', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'http://a/b/c/g;x?y#s', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'http://a:123/b/c/g;x?y#s', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'example-123.com', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'example-123.com', | ||
'requireScheme' => true, | ||
'valid' => false, | ||
], | ||
[ | ||
'url' => 'yup', | ||
'requireScheme' => false, | ||
'valid' => true, | ||
], | ||
[ | ||
'url' => 'nope', | ||
'requireScheme' => true, | ||
'valid' => false, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideValidate | ||
*/ | ||
public function testValidate(string $email, bool $requireScheme, bool $valid) | ||
{ | ||
$field = new UrlField('MyUrl'); | ||
$field->setValue($email); | ||
$field->setRequireScheme($requireScheme); | ||
$validator = new RequiredFields(); | ||
$field->validate($validator); | ||
$expectedCount = $valid ? 0 : 1; | ||
$this->assertEquals($expectedCount, count($validator->getErrors())); | ||
} | ||
} |