Skip to content

Instantly share code, notes, and snippets.

@janmarek
Created September 3, 2010 16:51
Show Gist options
  • Save janmarek/564169 to your computer and use it in GitHub Desktop.
Save janmarek/564169 to your computer and use it in GitHub Desktop.
Nette datepicker
<?php
class BaseForm extends Nette\Application\AppForm
{
public function addDatePicker($name, $label = NULL, $cols = NULL, $maxLength = NULL)
{
return $this[$name] = new Nette\Forms\DatePicker($label, $cols, $maxLength);
}
}
$("input.datepicker").livequery(function () {
$(this).datepicker();
});
<?php
namespace Nette\Forms;
use DateTime;
class DatePicker extends TextInput
{
/** @var string */
private $format = "j.n.Y";
public function __construct($label = NULL, $cols = NULL, $maxLength = NULL)
{
parent::__construct($label, $cols, $maxLength);
$this->setAttribute("class", "datepicker");
}
public function setValue($value)
{
if ($value instanceof DateTime) {
parent::setValue($value->format($this->format));
} else {
parent::setValue($value);
}
}
public function getValue()
{
$dateTime = DateTime::createFromFormat($this->format, parent::getValue());
return $dateTime ?: null;
}
public function getFormat()
{
return $this->format;
}
public function setFormat($format)
{
$this->format = $format;
}
public static function validateFilled(IFormControl $control)
{
return $control->getValue() instanceof DateTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment