Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Inputs/DateInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,27 @@ public function __construct($label = null, ?int $maxLength = null)

parent::__construct($label, null);

$this->addRule(fn (Control $input) => DateTimeFormat::validate($this->format, $input->getValue()), $this->invalidFormatMessage);
// must be a static callable, otherwise Nette::exportRules() stops at this rule
// and every rule/condition added later is missing in data-nette-rules
$this->addRule([self::class, 'validateFormat'], $this->invalidFormatMessage);

$this->setFormat(static::$defaultFormat);
}

/**
* Checks that the value matches the control's format.
*
* Kept as a public static method (instead of a closure) so that the rule is exportable
* and does not cut off the rules that follow it. The client side has no validator of
* this name and simply skips it.
*/
public static function validateFormat(Control $input): bool
{
assert($input instanceof self);

return DateTimeFormat::validate($input->getFormat(), $input->getValue());
}

/**
* @inheritdoc
*/
Expand Down
27 changes: 25 additions & 2 deletions tests/Inputs/DateInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,47 @@
use Contributte\FormsBootstrap\BootstrapForm;
use Contributte\FormsBootstrap\Enums\DateTimeFormat;
use Contributte\FormsBootstrap\Inputs\DateInput;
use Nette\Forms\Form;
use Tests\BaseTestCase;

class DateInputTest extends BaseTestCase
{

// rule that DateInput adds in its constructor
private const FORMAT_RULE = '[{"op":"Contributte\\\\FormsBootstrap\\\\Inputs\\\\DateInput::validateFormat","msg":"invalid/incorrect format"}]';

public function testDefaultDate(): void
{
$form = new BootstrapForm();
$dt = $form->addBootstrapDate('date', 'Date');
$this->assertEquals('<input type="text" name="date" id="frm-date" class="form-control" placeholder="d.m.yyyy (31.12.1998)">', $dt->getControl()->render());
$this->assertEquals('<input type="text" name="date" id="frm-date" data-nette-rules=\'' . self::FORMAT_RULE . '\' class="form-control" placeholder="d.m.yyyy (31.12.1998)">', $dt->getControl()->render());
}

public function testWithCustomStaticFormat(): void
{
$form = new BootstrapForm();
DateInput::$defaultFormat = DateTimeFormat::D_YMD_DASHES;
$dt = $form->addBootstrapDate('date', 'Date');
$this->assertEquals('<input type="text" name="date" id="frm-date" class="form-control" placeholder="yyyy-mm-dd (1998-12-31)">', $dt->getControl()->render());
$this->assertEquals('<input type="text" name="date" id="frm-date" data-nette-rules=\'' . self::FORMAT_RULE . '\' class="form-control" placeholder="yyyy-mm-dd (1998-12-31)">', $dt->getControl()->render());
}

/**
* @see https://github.com/contributte/forms-bootstrap/issues/61
*/
public function testConditionsAreExported(): void
{
$form = new BootstrapForm();
$checkbox = $form->addCheckbox('agree', 'Agree');
$date = $form->addBootstrapDate('date', 'Date');
$date->addConditionOn($checkbox, Form::Equal, true)
->setRequired('Date is required');
$date->addCondition(Form::Filled)
->addRule(Form::MinLength, 'Too short', 3);

$rules = $date->getControl()->render();
$this->assertStringContainsString(':equal', $rules);
$this->assertStringContainsString(':filled', $rules);
$this->assertStringContainsString(':minLength', $rules);
}

}
30 changes: 28 additions & 2 deletions tests/Inputs/DateTimeInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
class DateTimeInputTest extends BaseTestCase
{

// rule that DateInput adds in its constructor
private const FORMAT_RULE = '[{"op":"Contributte\\\\FormsBootstrap\\\\Inputs\\\\DateInput::validateFormat","msg":"invalid/incorrect format"}]';

public function testDefaultDateTime(): void
{
$form = new BootstrapForm();
$dt = $form->addBootstrapDateTime('datetime', 'Date and time');
$this->assertEquals('<input type="text" name="datetime" id="frm-datetime" class="form-control" placeholder="d.m.yyyy h:mm (31.12.1998 23:59)">', $dt->getControl()->render());
$this->assertEquals('<input type="text" name="datetime" id="frm-datetime" data-nette-rules=\'' . self::FORMAT_RULE . '\' class="form-control" placeholder="d.m.yyyy h:mm (31.12.1998 23:59)">', $dt->getControl()->render());
}

public function testDefaultAdditionalClasses(): void
Expand All @@ -24,7 +27,7 @@ public function testDefaultAdditionalClasses(): void
DateTimeInput::$additionalHtmlClasses[] = 'datetimepicker';
DateTimeInput::$additionalHtmlClasses[] = 'cool';
$dt = $form->addBootstrapDateTime('datetime', 'Date and time');
$this->assertEquals('<input type="text" name="datetime" id="frm-datetime" class="form-control datetimepicker cool" placeholder="d.m.yyyy h:mm (31.12.1998 23:59)">', $dt->getControl()->render());
$this->assertEquals('<input type="text" name="datetime" id="frm-datetime" data-nette-rules=\'' . self::FORMAT_RULE . '\' class="form-control datetimepicker cool" placeholder="d.m.yyyy h:mm (31.12.1998 23:59)">', $dt->getControl()->render());
}

public function testNotMessingHtmlClassOfDateAndDateTime(): void
Expand Down Expand Up @@ -62,4 +65,27 @@ public function testValidationWithCorrectFormat(): void
$this->assertEquals(new DateTime('2020-05-01'), $dt->getValue());
}

public function testValidationWithIncorrectFormat(): void
{
$form = new BootstrapForm();
$dt = $form->addBootstrapDateTime('datetime', 'Date and time');
$dt->setValue('not a date at all');
$submit = $form->addSubmit('send');
$form->setSubmittedBy($submit);
$form->validate();
$this->assertFalse($form->isValid());
$this->assertSame([$dt->invalidFormatMessage], $dt->getErrors());
}

public function testEmptyOptionalValueDoesNotFailValidation(): void
{
$form = new BootstrapForm();
$dt = $form->addBootstrapDateTime('datetime', 'Date and time');
$submit = $form->addSubmit('send');
$form->setSubmittedBy($submit);
$form->validate();
$this->assertTrue($form->isValid());
$this->assertSame([], $dt->getErrors());
}

}
Loading