Skip to content

Commit

Permalink
examples: improved custom-control.php
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 23, 2015
1 parent 9c8375f commit 4bd7408
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions examples/custom-control.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}

}
Expand Down

0 comments on commit 4bd7408

Please sign in to comment.