Skip to content

Commit

Permalink
NEW Add UrlField
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 31, 2024
1 parent 1eadc84 commit 4c975d4
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/Forms/UrlField.php
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);
}
}
120 changes: 120 additions & 0 deletions tests/php/Forms/UrlFieldTest.php
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()));
}
}

0 comments on commit 4c975d4

Please sign in to comment.