Skip to content

Commit 1b4d03a

Browse files
committed
examples: improved custom-control.php
1 parent d06dacb commit 1b4d03a

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

examples/custom-control.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
class DateInput extends Nette\Forms\Controls\BaseControl
1717
{
18-
private $day, $month, $year;
18+
/** @var string */
19+
private
20+
$day = '',
21+
$month = '',
22+
$year = '';
1923

2024

2125
public function __construct($label = NULL)
@@ -28,7 +32,7 @@ public function __construct($label = NULL)
2832
public function setValue($value)
2933
{
3034
if ($value === NULL) {
31-
$this->day = $this->month = $this->year = NULL;
35+
$this->day = $this->month = $this->year = '';
3236
} else {
3337
$date = Nette\Utils\DateTime::from($value);
3438
$this->day = $date->format('j');
@@ -50,6 +54,15 @@ public function getValue()
5054
}
5155

5256

57+
/**
58+
* @return bool
59+
*/
60+
public function isFilled()
61+
{
62+
return $this->day !== '' || $this->year !== '';
63+
}
64+
65+
5366
public function loadHttpData()
5467
{
5568
$this->day = $this->getHttpData(Form::DATA_LINE, '[day]');
@@ -79,9 +92,9 @@ public function getControl()
7992
*/
8093
public static function validateDate(Nette\Forms\IControl $control)
8194
{
82-
return is_numeric($control->day)
83-
&& is_numeric($control->month)
84-
&& is_numeric($control->year)
95+
return ctype_digit($control->day)
96+
&& ctype_digit($control->month)
97+
&& ctype_digit($control->year)
8598
&& checkdate($control->month, $control->day, $control->year);
8699
}
87100

0 commit comments

Comments
 (0)