-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validation rules added using the addRule(Form::Pattern, ...) call, not using any custom validation callback because I want the JS validation to be also used and don't want to add a custom one.
- Loading branch information
Showing
8 changed files
with
91 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Form; | ||
|
||
use Contributte\Translation\Translator; | ||
use Nette\Forms\Controls\TextInput; | ||
use Nette\Forms\Form; | ||
|
||
readonly class FormValidators | ||
{ | ||
|
||
public function __construct( | ||
private Translator $translator, | ||
) { | ||
} | ||
|
||
|
||
public function addValidateSlugRules(TextInput $input): void | ||
{ | ||
$input->addRule(Form::Pattern, $this->translator->translate('messages.forms.validateSlugParamsError'), '[a-z0-9.,_-]+'); | ||
} | ||
|
||
} |
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
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,52 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Form; | ||
|
||
use MichalSpacekCz\Test\TestCaseRunner; | ||
use Nette\Forms\Controls\TextInput; | ||
use Nette\Forms\Form; | ||
use Tester\Assert; | ||
use Tester\TestCase; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
/** @testCase */ | ||
class FormValidatorsTest extends TestCase | ||
{ | ||
|
||
public function __construct( | ||
private readonly FormValidators $validators, | ||
) { | ||
} | ||
|
||
|
||
public function getSlugs(): array | ||
{ | ||
return [ | ||
['foo', true], | ||
['foo-bar', true], | ||
['foo-bar.baz', true], | ||
['foo-bar,baz', true], | ||
['foo-bar_baz', true], | ||
['foo-bar-1337', true], | ||
['foo/bar', false], | ||
]; | ||
} | ||
|
||
|
||
/** @dataProvider getSlugs */ | ||
public function testAddValidateSlugRules(string $slug, bool $result): void | ||
{ | ||
$input = new TextInput(); | ||
$input->value = $slug; | ||
$this->validators->addValidateSlugRules($input); | ||
/** @noinspection PhpInternalEntityUsedInspection */ | ||
$input->setParent(new Form()); | ||
$input->validate(); | ||
Assert::same($result ? [] : ['messages.forms.validateSlugParamsError'], $input->getErrors()); | ||
} | ||
|
||
} | ||
|
||
TestCaseRunner::run(FormValidatorsTest::class); |