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 30, 2024
1 parent 1eadc84 commit 2223cde
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Forms/UrlField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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;
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);
}
}
100 changes: 100 additions & 0 deletions tests/php/Forms/UrlFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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.com',
'requireScheme' => false,
'valid' => true,
],
[
'url' => 'https://example.com',
'requireScheme' => false,
'valid' => true,
],
[
'url' => 'ftp://example.com',
'requireScheme' => false,
'valid' => true,
],
[
'url' => 'http://example.com:8080',
'requireScheme' => false,
'valid' => true,
],
[
'url' => 'http://example.com:8080/path_with_underscores_(and)_parens',
'requireScheme' => false,
'valid' => true,
],
[
'url' => 'http://example.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.com',
'requireScheme' => false,
'valid' => true,
],
[
'url' => 'example.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 2223cde

Please sign in to comment.