Skip to content

Commit

Permalink
BC: Compatibility with nette/forms 3.1.14
Browse files Browse the repository at this point in the history
renamed addDate, addDateTime to addBootstrapDate and addBootstrapDateTime.
added Date/DateTime/Time/Color inputs which acts as default nette forms
  • Loading branch information
dakorpar committed Mar 12, 2024
1 parent 0fcc668 commit 649a442
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"require": {
"php": ">=8.1",
"nette/forms": "3.1.12",
"nette/forms": "3.1.14",
"nette/application": "^3.0"
},
"require-dev": {
Expand Down
25 changes: 13 additions & 12 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<phpunit bootstrap="tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="all">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="all">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
37 changes: 37 additions & 0 deletions src/Inputs/ColorPicker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);

namespace Contributte\FormsBootstrap\Inputs;

use Contributte\FormsBootstrap\BootstrapUtils;
use Contributte\FormsBootstrap\Traits\StandardValidationTrait;
use Nette\Utils\Html;

class ColorPicker extends \Nette\Forms\Controls\ColorPicker
{

use StandardValidationTrait;

/** @var bool */
private $isValidated = false;

Check failure on line 15 in src/Inputs/ColorPicker.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.1, ubuntu-latest)

Property Contributte\FormsBootstrap\Inputs\ColorPicker::$isValidated is never read, only written.

Check failure on line 15 in src/Inputs/ColorPicker.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.1, ubuntu-latest)

Property Contributte\FormsBootstrap\Inputs\ColorPicker::$isValidated is never read, only written.

public function getControl(): Html
{
$control = parent::getControl();
BootstrapUtils::standardizeClass($control);

$control->class[] = 'form-control';

return $control;
}

/**
* @inheritdoc
*/
public function validate(): void
{
parent::validate();

$this->isValidated = true;
}

}
45 changes: 45 additions & 0 deletions src/Inputs/DateTimeControl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace Contributte\FormsBootstrap\Inputs;

use Contributte\FormsBootstrap\BootstrapUtils;
use Contributte\FormsBootstrap\Traits\StandardValidationTrait;
use Nette\Utils\Html;

class DateTimeControl extends \Nette\Forms\Controls\DateTimeControl
{

use StandardValidationTrait;

/**
* Input accepted format.
* Default is d.m.yyyy
*
* @var string
*/
private $format;

Check failure on line 20 in src/Inputs/DateTimeControl.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.1, ubuntu-latest)

Property Contributte\FormsBootstrap\Inputs\DateTimeControl::$format is unused.

Check failure on line 20 in src/Inputs/DateTimeControl.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.1, ubuntu-latest)

Property Contributte\FormsBootstrap\Inputs\DateTimeControl::$format is unused.

/** @var bool */
private $isValidated = false;

Check failure on line 23 in src/Inputs/DateTimeControl.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.1, ubuntu-latest)

Property Contributte\FormsBootstrap\Inputs\DateTimeControl::$isValidated is never read, only written.

Check failure on line 23 in src/Inputs/DateTimeControl.php

View workflow job for this annotation

GitHub Actions / Static analysis (8.1, ubuntu-latest)

Property Contributte\FormsBootstrap\Inputs\DateTimeControl::$isValidated is never read, only written.

public function getControl(): Html
{
$control = parent::getControl();
BootstrapUtils::standardizeClass($control);

$control->class[] = 'form-control';

return $control;
}

/**
* @inheritdoc
*/
public function validate(): void
{
parent::validate();

$this->isValidated = true;
}

}
62 changes: 59 additions & 3 deletions src/Traits/BootstrapContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Contributte\FormsBootstrap\Inputs\ButtonInput;
use Contributte\FormsBootstrap\Inputs\CheckboxInput;
use Contributte\FormsBootstrap\Inputs\CheckboxListInput;
use Contributte\FormsBootstrap\Inputs\ColorPicker;
use Contributte\FormsBootstrap\Inputs\DateInput;
use Contributte\FormsBootstrap\Inputs\DateTimeControl;
use Contributte\FormsBootstrap\Inputs\DateTimeInput;
use Contributte\FormsBootstrap\Inputs\MultiselectInput;
use Contributte\FormsBootstrap\Inputs\RadioInput;
Expand All @@ -22,6 +24,8 @@
use Nette\Forms\Controls\Button;
use Nette\Forms\Controls\Checkbox;
use Nette\Forms\Controls\CheckboxList;
use Nette\Forms\Controls\ColorPicker as NetteColorPicker;
use Nette\Forms\Controls\DateTimeControl as NetteDateTimeControl;
use Nette\Forms\Controls\MultiSelectBox;
use Nette\Forms\Controls\RadioList;
use Nette\Forms\Controls\SelectBox;
Expand Down Expand Up @@ -99,12 +103,64 @@ public function addContainer($name): Container
return $control;
}

/**
* Adds a datetime input.
*
* @param string|null $label
*/
public function addDate(string $name, $label = null): NetteDateTimeControl
{
$comp = new DateTimeControl($label, NetteDateTimeControl::TypeDate);
$this->addComponent($comp, $name);

return $comp;
}

/**
* Adds a datetime input.
*
* @param string|Html|null $label
*/
public function addDateTime(string $name, $label = null, bool $withSeconds = false): NetteDateTimeControl
{
$comp = new DateTimeControl($label, NetteDateTimeControl::TypeDateTime);
$this->addComponent($comp, $name);

return $comp;
}

/**
* Adds a time input
*
* @param string|null $label
*/
public function addTime(string $name, $label = null, bool $withSeconds = false): NetteDateTimeControl
{
$comp = new DateTimeControl($label, NetteDateTimeControl::TypeTime);
$this->addComponent($comp, $name);

return $comp;
}

/**
* Adds a color input
*
* @param string|null $label
*/
public function addColor(string $name, $label = null, bool $withSeconds = false): NetteColorPicker
{
$comp = new ColorPicker($label);
$this->addComponent($comp, $name);

return $comp;
}

/**
* Adds a datetime input.
*/
public function addDate(string $name, string $label): DateInput
public function addBootstrapDate(string $name, string $label): DateInput
{
$comp = new DateInput($label, null);
$comp = new DateInput($label);
$comp->setNullable(BootstrapForm::$allwaysUseNullable);
$this->addComponent($comp, $name);

Expand All @@ -115,7 +171,7 @@ public function addDate(string $name, string $label): DateInput
* @param string|Html|null $label
* Adds a datetime input.
*/
public function addDateTime(string $name, $label): DateTimeInput
public function addBootstrapDateTime(string $name, $label): DateTimeInput
{
$comp = new DateTimeInput($label);
$comp->setNullable(BootstrapForm::$allwaysUseNullable);
Expand Down
19 changes: 19 additions & 0 deletions tests/Inputs/ColorPickerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare (strict_types = 1);

namespace Tests\Inputs;

use Contributte\FormsBootstrap\BootstrapForm;
use Tests\BaseTest;

class ColorPickerTest extends BaseTest
{

public function testDefaultTextInput(): void
{
$form = new BootstrapForm();
$input = $form->addColor('color', 'Choose color');
$this->assertEquals('<input type="color" name="color" id="frm-color" value="#000000" class="form-control">', $input->getControl()->render());
$this->assertEquals('<label for="frm-color">Choose color</label>', (string) $input->getLabel());
}

}
4 changes: 2 additions & 2 deletions tests/Inputs/DateInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class DateInputTest extends BaseTest
public function testDefaultDate(): void
{
$form = new BootstrapForm();
$dt = $form->addDate('date', 'Date');
$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());
}

public function testWithCustomStaticFormat(): void
{
$form = new BootstrapForm();
DateInput::$defaultFormat = DateTimeFormat::D_YMD_DASHES;
$dt = $form->addDate('date', 'Date');
$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());
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Inputs/DateTimeControlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare (strict_types = 1);

namespace Tests\Inputs;

use Contributte\FormsBootstrap\BootstrapForm;
use Tests\BaseTest;

class DateTimeControlTest extends BaseTest
{

public function testDefaultDate(): void
{
$form = new BootstrapForm();
$dt = $form->addDate('date', 'Date');
$this->assertEquals('<input type="date" name="date" id="frm-date" class="form-control">', $dt->getControl()->render());
}

}
12 changes: 6 additions & 6 deletions tests/Inputs/DateTimeInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DateTimeInputTest extends BaseTest
public function testDefaultDateTime(): void
{
$form = new BootstrapForm();
$dt = $form->addDateTime('datetime', 'Date and time');
$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());
}

Expand All @@ -23,7 +23,7 @@ public function testDefaultAdditionalClasses(): void
$form = new BootstrapForm();
DateTimeInput::$additionalHtmlClasses[] = 'datetimepicker';
DateTimeInput::$additionalHtmlClasses[] = 'cool';
$dt = $form->addDateTime('datetime', 'Date and time');
$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());
}

Expand All @@ -33,17 +33,17 @@ public function testNotMessingHtmlClassOfDateAndDateTime(): void
DateInput::$additionalHtmlClasses = ['date'];

$form = new BootstrapForm();
$dt = $form->addDateTime('datetime', 'Date and time');
$dt = $form->addBootstrapDateTime('datetime', 'Date and time');
$this->assertStringContainsString('class="form-control datetime"', $dt->getControl()->render());

$d = $form->addDate('date', 'Date');
$d = $form->addBootstrapDate('date', 'Date');
$this->assertStringContainsString('class="form-control date"', $d->getControl()->render());
}

public function testValidationWithValueFromDatabase(): void
{
$form = new BootstrapForm();
$dt = $form->addDateTime('datetime', 'Date and time');
$dt = $form->addBootstrapDateTime('datetime', 'Date and time');
$dt->setValue('2020-05-05 20:00:00');
$form->validate();
$this->assertEquals(new DateTime('2020-05-05 20:00:00'), $dt->getValue());
Expand All @@ -52,7 +52,7 @@ public function testValidationWithValueFromDatabase(): void
public function testValidationWithCorrectFormat(): void
{
$form = new BootstrapForm();
$dt = $form->addDateTime('datetime', 'Date and time');
$dt = $form->addBootstrapDateTime('datetime', 'Date and time');
$dt->setValue((new DateTime('2020-05-01'))->format($dt->format));
$form->validate();
$this->assertEquals(new DateTime('2020-05-01'), $dt->getValue());
Expand Down

0 comments on commit 649a442

Please sign in to comment.