File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments