Skip to content

Commit

Permalink
breaks exporting rules to JS after non-static validator [Closes #259]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 19, 2021
1 parent 8cab472 commit 1592cf6
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Forms/Controls/TextBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ protected function getRenderedValue(): ?string
/** @return static */
public function addRule($validator, $errorMessage = null, $arg = null)
{
foreach ($this->getRules() as $rule) {
if (!$rule->canExport() && !$rule->branch) {
return $this;
}
}

if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) {
$tmp = is_array($arg) ? $arg[1] : $arg;
if (is_scalar($tmp)) {
Expand Down
6 changes: 6 additions & 0 deletions src/Forms/Controls/TextInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public function getControl(): Nette\Utils\Html
/** @return static */
public function addRule($validator, $errorMessage = null, $arg = null)
{
foreach ($this->getRules() as $rule) {
if (!$rule->canExport() && !$rule->branch) {
return $this;
}
}

if ($this->control->type === null && in_array($validator, [Form::EMAIL, Form::URL, Form::INTEGER], true)) {
static $types = [Form::EMAIL => 'email', Form::URL => 'url', Form::INTEGER => 'number'];
$this->control->type = $types[$validator];
Expand Down
3 changes: 2 additions & 1 deletion src/Forms/Controls/UploadControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function __construct($label = null, bool $multiple = false)
$this->control->type = 'file';
$this->control->multiple = $multiple;
$this->setOption('type', 'file');
$this->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
$this->addCondition(true) // not to block the export of rules to JS
->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
$this->addRule(Form::MAX_FILE_SIZE, null, Forms\Helpers::iniGetSize('upload_max_filesize'));

$this->monitor(Form::class, function (Form $form): void {
Expand Down
10 changes: 8 additions & 2 deletions src/Forms/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,18 @@ public static function exportRules(Rules $rules): array
{
$payload = [];
foreach ($rules as $rule) {
if (!is_string($op = $rule->validator)) {
if (!Nette\Utils\Callback::isStatic($op)) {
if (!$rule->canExport()) {
if ($rule->branch) {
continue;
}
break;
}

$op = $rule->validator;
if (!is_string($op)) {
$op = Nette\Utils\Callback::toString($op);
}

if ($rule->branch) {
$item = [
'op' => ($rule->isNegative ? '~' : '') . $op,
Expand Down
8 changes: 8 additions & 0 deletions src/Forms/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ class Rule

/** @var Rules|null for conditions */
public $branch;


/** @internal */
public function canExport(): bool
{
return is_string($this->validator)
|| Nette\Utils\Callback::isStatic($this->validator);
}
}
10 changes: 10 additions & 0 deletions tests/Forms/Controls.TextArea.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,13 @@ test('setEmptyValue & setNullable', function () {
Assert::null($input->getValue());
Assert::same('<textarea name="text" id="frm-text" data-nette-empty-value="empty">empty </textarea>', (string) $input->getControl());
});


test('addFilter() & rules', function () {
$form = new Form;
$input = $form->addTextArea('text')
->addFilter(function () {})
->addRule(Form::MAX_LENGTH, 'maxl', 10);

Assert::same('<textarea name="text" id="frm-text"></textarea>', (string) $input->getControl());
});
12 changes: 12 additions & 0 deletions tests/Forms/Controls.TextInput.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,15 @@ test('addInteger', function () {

Assert::same('<input type="number" name="text" id="frm-text" data-nette-rules=\'[{"op":":integer","msg":"Please enter a valid integer."}]\'>', (string) $input->getControl());
});


test('addFilter() & rules', function () {
$form = new Form;
$input = $form->addText('text')
->addRule(Form::MIN, 'min', 1)
->addFilter(function () {})
->addRule(Form::MAX, 'max', 10)
->addRule(Form::MAX_LENGTH, 'maxl', 10);

Assert::same('<input type="text" name="text" id="frm-text" data-nette-rules=\'[{"op":":min","msg":"min","arg":1}]\'>', (string) $input->getControl());
});
13 changes: 13 additions & 0 deletions tests/Forms/Helpers.exportRules.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,16 @@ test('', function () {
],
], Helpers::exportRules($input2->getRules()));
});


test('addFilter', function () {
$form = new Form;
$input = $form->addText('text');
$input->addRule(Form::PATTERN, 'match pattern', '\d+');
$input->addFilter(function () {});
$input->setRequired(false);
$input->addRule(Form::EMAIL);
Assert::same([
['op' => ':pattern', 'msg' => 'match pattern', 'arg' => '\\d+'],
], Helpers::exportRules($input->getRules()));
});

0 comments on commit 1592cf6

Please sign in to comment.