Skip to content

Commit 81a6be3

Browse files
committed
Added sanitize to email and integer field
1 parent f1c7040 commit 81a6be3

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

src/Fields/CharField.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public function __construct(array $args = array())
3232
}
3333
}
3434

35+
public function toNative($value)
36+
{
37+
$value = parent::toNative($value);
38+
39+
$value = trim((string) $value);
40+
41+
return !empty($value) ? $value : '';
42+
}
43+
3544
/**
3645
* Return extra minlength and maxlength attrs to be added to the input in HTML.
3746
*

src/Fields/EmailField.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ public function __construct(array $args = array())
1717

1818
$this->validators[] = new EmailValidator();
1919
}
20+
21+
public function toNative($value)
22+
{
23+
$value = parent::toNative($value);
24+
25+
return filter_var($value, FILTER_SANITIZE_EMAIL);
26+
}
2027
}

src/Fields/IntegerField.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function __construct(array $args = array())
3535

3636
public function toNative($value)
3737
{
38+
$value = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
39+
3840
if (is_numeric($value)) {
3941
return intval($value);
4042
} else {

tests/unit/Fields/CharFieldTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,11 @@ public function testWidgetAttrs()
5151
$expected = array("minlength" => 10, "maxlength" => 20);
5252
$this->assertEquals($expected, $this->field->widgetAttrs(null));
5353
}
54+
55+
public function testToNative()
56+
{
57+
$this->assertEquals("text", $this->field->toNative("text"));
58+
$this->assertEquals("text", $this->field->toNative(" text "));
59+
$this->assertEquals("", $this->field->toNative(0));
60+
}
5461
}

0 commit comments

Comments
 (0)