Skip to content

Commit 6f8c9d1

Browse files
committed
examples: improved custom-control.php
1 parent f7b7b57 commit 6f8c9d1

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

examples/custom-control.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ public function __construct($label = NULL)
2727

2828
public function setValue($value)
2929
{
30-
if ($value) {
30+
if ($value === NULL) {
31+
$this->day = $this->month = $this->year = NULL;
32+
} else {
3133
$date = Nette\Utils\DateTime::from($value);
3234
$this->day = $date->format('j');
3335
$this->month = $date->format('n');
3436
$this->year = $date->format('Y');
35-
} else {
36-
$this->day = $this->month = $this->year = NULL;
3737
}
38+
return $this;
3839
}
3940

4041

@@ -44,7 +45,7 @@ public function setValue($value)
4445
public function getValue()
4546
{
4647
return self::validateDate($this)
47-
? date_create()->setDate($this->year, $this->month, $this->day)
48+
? (new DateTime)->setDate($this->year, $this->month, $this->day)->setTime(0, 0)
4849
: NULL;
4950
}
5051

@@ -78,7 +79,10 @@ public function getControl()
7879
*/
7980
public static function validateDate(Nette\Forms\IControl $control)
8081
{
81-
return checkdate($control->month, $control->day, $control->year);
82+
return is_numeric($control->day)
83+
&& is_numeric($control->month)
84+
&& is_numeric($control->year)
85+
&& checkdate($control->month, $control->day, $control->year);
8286
}
8387

8488
}

0 commit comments

Comments
 (0)