Skip to content
This repository has been archived by the owner on Nov 15, 2020. It is now read-only.

Commit

Permalink
If is control required validate date/datetime too
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtak-CZ committed Apr 20, 2015
1 parent bf3666c commit e7afe1d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Nella/Forms/DateTime/DateInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Nella\Forms\DateTime;

use Nette\Forms\Container;
use Nette\Forms\Form;

/**
* Date input form control
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions src/Nella/Forms/DateTime/DateTimeInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions tests/Nella/Forms/DateTime/DateInputTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/Nella/Forms/DateTime/DateTimeInputTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit e7afe1d

Please sign in to comment.