Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
246 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Nepada\FormRenderer\Filters; | ||
|
||
use Nette; | ||
|
||
final class ValidationClassFilter | ||
{ | ||
|
||
use Nette\SmartObject; | ||
|
||
private ?string $invalidClass; | ||
|
||
private ?string $validClass; | ||
|
||
public function __construct(?string $invalidClass, ?string $validClass) | ||
{ | ||
$this->invalidClass = $invalidClass; | ||
$this->validClass = $validClass; | ||
} | ||
|
||
public function __invoke(Nette\Forms\IControl $control): ?string | ||
{ | ||
if (count($control->getErrors()) > 0) { | ||
return $this->invalidClass; | ||
} | ||
|
||
if ($this->isFilled($control)) { | ||
return $this->validClass; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function isFilled(Nette\Forms\IControl $control): bool | ||
{ | ||
if ($control instanceof Nette\Forms\Controls\BaseControl || method_exists($control, 'isFilled')) { | ||
return $control->isFilled(); | ||
} | ||
|
||
$value = $control->getValue(); | ||
return $value !== null && $value !== [] && $value !== ''; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace NepadaTests\FormRenderer\Filters\Fixtures; | ||
|
||
use Nette; | ||
|
||
class CustomControl implements Nette\Forms\IControl | ||
{ | ||
|
||
use Nette\SmartObject; | ||
|
||
/** @var mixed */ | ||
private $value; | ||
|
||
/** @var string[] */ | ||
private array $errors; | ||
|
||
/** | ||
* @param mixed $value | ||
* @param string[] $errors | ||
*/ | ||
public function __construct($value = null, array $errors = []) | ||
{ | ||
$this->value = $value; | ||
$this->errors = $errors; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @return static | ||
*/ | ||
public function setValue($value): self | ||
{ | ||
$this->value = $value; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function validate(): void | ||
{ | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getErrors(): array | ||
{ | ||
return $this->errors; | ||
} | ||
|
||
public function isOmitted(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
tests/FormRenderer/Filters/Fixtures/FillableCustomControl.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace NepadaTests\FormRenderer\Filters\Fixtures; | ||
|
||
class FillableCustomControl extends CustomControl | ||
{ | ||
|
||
private bool $isFilled; | ||
|
||
/** | ||
* @param bool $isFilled | ||
* @param mixed $value | ||
* @param string[] $errors | ||
*/ | ||
public function __construct(bool $isFilled, $value = null, array $errors = []) | ||
{ | ||
parent::__construct($value, $errors); | ||
$this->isFilled = $isFilled; | ||
} | ||
|
||
public function isFilled(): bool | ||
{ | ||
return $this->isFilled; | ||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
tests/FormRenderer/Filters/ValidationClassFilterTest.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace NepadaTests\FormRenderer\Filters; | ||
|
||
use Nepada\FormRenderer\Filters\ValidationClassFilter; | ||
use NepadaTests\FormRenderer\Filters\Fixtures\CustomControl; | ||
use NepadaTests\FormRenderer\Filters\Fixtures\FillableCustomControl; | ||
use NepadaTests\TestCase; | ||
use Nette\Forms\IControl; | ||
use Tester\Assert; | ||
|
||
require_once __DIR__ . '/../../bootstrap.php'; | ||
|
||
|
||
/** | ||
* @testCase | ||
*/ | ||
class ValidationClassFilterTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @dataProvider getFilterData | ||
* @param string $description | ||
* @param string|null $invalidClass | ||
* @param string|null $validClass | ||
* @param IControl $control | ||
* @param string|null $expectedClass | ||
*/ | ||
public function testFilter(string $description, ?string $invalidClass, ?string $validClass, IControl $control, ?string $expectedClass): void | ||
{ | ||
$filter = new ValidationClassFilter($invalidClass, $validClass); | ||
Assert::same($expectedClass, $filter->__invoke($control), $description); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
protected function getFilterData(): array | ||
{ | ||
return [ | ||
[ | ||
'description' => 'filled control with error', | ||
'invalidClass' => 'error', | ||
'validClass' => 'filled', | ||
'control' => new FillableCustomControl(true, 'foo', ['error']), | ||
'expectedClass' => 'error', | ||
], | ||
[ | ||
'description' => 'filled control with error', | ||
'invalidClass' => null, | ||
'validClass' => 'filled', | ||
'control' => new FillableCustomControl(true, 'foo', ['error']), | ||
'expectedClass' => null, | ||
], | ||
[ | ||
'description' => 'isFilled() preference', | ||
'invalidClass' => 'error', | ||
'validClass' => 'filled', | ||
'control' => new FillableCustomControl(false, 'foo'), | ||
'expectedClass' => null, | ||
], | ||
[ | ||
'description' => 'isFilled() preference', | ||
'invalidClass' => 'error', | ||
'validClass' => 'filled', | ||
'control' => new FillableCustomControl(true, null), | ||
'expectedClass' => 'filled', | ||
], | ||
[ | ||
'description' => 'null is not filled by fallback logic', | ||
'invalidClass' => 'error', | ||
'validClass' => 'filled', | ||
'control' => new CustomControl(null), | ||
'expectedClass' => null, | ||
], | ||
[ | ||
'description' => 'empty array is not filled by fallback logic', | ||
'invalidClass' => 'error', | ||
'validClass' => 'filled', | ||
'control' => new CustomControl([]), | ||
'expectedClass' => null, | ||
], | ||
[ | ||
'description' => 'empty string is not filled by fallback logic', | ||
'invalidClass' => 'error', | ||
'validClass' => 'filled', | ||
'control' => new CustomControl(''), | ||
'expectedClass' => null, | ||
], | ||
[ | ||
'description' => 'filled by fallback logic', | ||
'invalidClass' => 'error', | ||
'validClass' => 'filled', | ||
'control' => new CustomControl('foo'), | ||
'expectedClass' => 'filled', | ||
], | ||
]; | ||
} | ||
|
||
} | ||
|
||
|
||
(new ValidationClassFilterTest())->run(); |