Skip to content

Commit 9edbf95

Browse files
committed
Added URLField definition
1 parent 81a6be3 commit 9edbf95

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/Fields/URLField.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* URLField Class
4+
*/
5+
namespace PHPForm\Fields;
6+
7+
use PHPForm\Exceptions\ValidationError;
8+
use PHPForm\Validators\URLValidator;
9+
use PHPForm\Widgets\URLInput;
10+
11+
class URLField extends CharField
12+
{
13+
protected $widget = URLInput::class;
14+
15+
public function __construct(array $args = array())
16+
{
17+
parent::__construct($args);
18+
19+
$this->validators[] = new URLValidator();
20+
}
21+
22+
public function toNative($value)
23+
{
24+
$value = parent::toNative($value);
25+
26+
return filter_var($value, FILTER_SANITIZE_URL);
27+
}
28+
}

tests/unit/Fields/URLFieldTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace PHPForm\Unit\Fields;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
use PHPForm\Exceptions\ValidationError;
7+
use PHPForm\Fields\URLField;
8+
use PHPForm\Widgets\URLInput;
9+
10+
class URLFieldTest extends TestCase
11+
{
12+
public function setUp()
13+
{
14+
$this->field = new URLField();
15+
}
16+
17+
public function testConstruct()
18+
{
19+
$this->assertInstanceOf(URLInput::class, $this->field->getWidget());
20+
}
21+
22+
/**
23+
* @expectedException PHPForm\Exceptions\ValidationError
24+
*/
25+
public function testURLValidator()
26+
{
27+
$this->field->clean("invalid url");
28+
}
29+
30+
public function testToNative()
31+
{
32+
$this->assertEquals("invalidurl", $this->field->toNative("invalid url"));
33+
$this->assertEquals("invalidrl", $this->field->toNative("invalid úrl"));
34+
}
35+
}

0 commit comments

Comments
 (0)