diff --git a/src/Nella/Forms/DateTime/DateInput.php b/src/Nella/Forms/DateTime/DateInput.php index 58425bd..a879669 100644 --- a/src/Nella/Forms/DateTime/DateInput.php +++ b/src/Nella/Forms/DateTime/DateInput.php @@ -11,6 +11,7 @@ namespace Nella\Forms\DateTime; use Nette\Forms\Container; +use Nette\Forms\Form; /** * Date input form control @@ -168,6 +169,26 @@ private function normalizeFormat($input) return \Nette\Utils\Strings::replace($input, '~\s+~', ''); } + /** + * @param string $message + * @return \Nella\Forms\DateTime\DateInput + */ + public function setRequired($message = TRUE) + { + if (!is_string($message)) { + throw new \Nette\InvalidArgumentException('Message must be string'); + } + + parent::setRequired($message); + + $this->addCondition(Form::FILLED) + ->addRule(function(DateInput $control) { + return $this->validateDate($control); + }, $message); + + return $this; + } + public static function register() { if (static::$registered) { diff --git a/src/Nella/Forms/DateTime/DateTimeInput.php b/src/Nella/Forms/DateTime/DateTimeInput.php index 4231475..2db9b4e 100644 --- a/src/Nella/Forms/DateTime/DateTimeInput.php +++ b/src/Nella/Forms/DateTime/DateTimeInput.php @@ -315,6 +315,26 @@ private function normalizeFormat($input) return \Nette\Utils\Strings::replace($input, '~\s+~', ''); } + /** + * @param string $message + * @return \Nella\Forms\DateTime\DateInput + */ + public function setRequired($message = TRUE) + { + if (!is_string($message)) { + throw new \Nette\InvalidArgumentException('Message must be string'); + } + + parent::setRequired($message); + + $this->addCondition(Form::FILLED) + ->addRule(function(DateTimeInput $control) { + return $this->validateDateTime($control); + }, $message); + + return $this; + } + public static function register() { if (static::$registered) { diff --git a/tests/Nella/Forms/DateTime/DateInputTest.phpt b/tests/Nella/Forms/DateTime/DateInputTest.phpt index 10590eb..3482cfa 100644 --- a/tests/Nella/Forms/DateTime/DateInputTest.phpt +++ b/tests/Nella/Forms/DateTime/DateInputTest.phpt @@ -233,6 +233,33 @@ class DateInputTest extends \Tester\TestCase Assert::equal(array('test'), $control->getErrors()); } + public function testInvalidRequired() + { + $control = $this->createControl(array( + 'date' => '2012-02-31', + ), true); + + $control->setRequired('test'); + + Assert::true($control->isFilled()); + Assert::null($control->getValue()); + + $control->validate(); + + Assert::true($control->hasErrors()); + Assert::equal(array('test'), $control->getErrors()); + } + + /** + * @throws \Nette\InvalidArgumentException + */ + public function testRequiredInvalidMessage() + { + $control = $this->createControl(); + + $control->setRequired(); + } + /** * @throws \Nette\InvalidStateException */ diff --git a/tests/Nella/Forms/DateTime/DateTimeInputTest.phpt b/tests/Nella/Forms/DateTime/DateTimeInputTest.phpt index f604aef..50704a0 100644 --- a/tests/Nella/Forms/DateTime/DateTimeInputTest.phpt +++ b/tests/Nella/Forms/DateTime/DateTimeInputTest.phpt @@ -467,6 +467,36 @@ class DateTimeInputTest extends \Tester\TestCase Assert::true($dq->has("input[value='12:00']")); } + public function testInvalidRequired() + { + $control = $this->createControl(array( + 'datetime' => array( + 'date' => '2012-02-31', + 'time' => '25:61', + ), + ), true); + + $control->setRequired('test'); + + Assert::true($control->isFilled()); + Assert::null($control->getValue()); + + $control->validate(); + + Assert::true($control->hasErrors()); + Assert::equal(array('test'), $control->getErrors()); + } + + /** + * @throws \Nette\InvalidArgumentException + */ + public function testRequiredInvalidMessage() + { + $control = $this->createControl(); + + $control->setRequired(); + } + /** * @throws \Nette\InvalidStateException */