From 4bd740877870d6873a65b2065deabadcf513541e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 20 Jul 2015 01:17:47 +0200 Subject: [PATCH] examples: improved custom-control.php --- examples/custom-control.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/custom-control.php b/examples/custom-control.php index 1942c447e..f37013259 100644 --- a/examples/custom-control.php +++ b/examples/custom-control.php @@ -27,14 +27,15 @@ public function __construct($label = NULL) public function setValue($value) { - if ($value) { + if ($value === NULL) { + $this->day = $this->month = $this->year = NULL; + } else { $date = Nette\Utils\DateTime::from($value); $this->day = $date->format('j'); $this->month = $date->format('n'); $this->year = $date->format('Y'); - } else { - $this->day = $this->month = $this->year = NULL; } + return $this; } @@ -44,7 +45,7 @@ public function setValue($value) public function getValue() { return self::validateDate($this) - ? date_create()->setDate($this->year, $this->month, $this->day) + ? date_create()->setDate($this->year, $this->month, $this->day)->setTime(0, 0) : NULL; } @@ -78,7 +79,10 @@ public function getControl() */ public static function validateDate(Nette\Forms\IControl $control) { - return checkdate($control->month, $control->day, $control->year); + return is_numeric($control->day) + && is_numeric($control->month) + && is_numeric($control->year) + && checkdate($control->month, $control->day, $control->year); } }