Skip to content

Commit

Permalink
Rules: allow Form::VALID only in the addConditionOn [fixes nette#95]
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Dec 3, 2015
1 parent ac902b1 commit cf8227a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Forms/Rules.php
Expand Up @@ -75,6 +75,9 @@ public function isRequired()
*/
public function addRule($validator, $message = NULL, $arg = NULL)
{
if ($validator === Form::VALID || ~$validator === Form::VALID) {
throw new Nette\InvalidArgumentException('You cannot use Form::VALID in the addRule method.');
}
$rule = new Rule;
$rule->control = $this->control;
$rule->validator = $validator;
Expand All @@ -98,6 +101,9 @@ public function addRule($validator, $message = NULL, $arg = NULL)
*/
public function addCondition($validator, $arg = NULL)
{
if ($validator === Form::VALID || ~$validator === Form::VALID) {
throw new Nette\InvalidArgumentException('You cannot use Form::VALID in the addCondition method.');
}
return $this->addConditionOn($this->control, $validator, $arg);
}

Expand Down
65 changes: 65 additions & 0 deletions tests/Forms/Rules.valid.phpt
@@ -0,0 +1,65 @@
<?php

/**
* Test: Nette\Forms\Rules.
*/

use Nette\Forms\Form;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test(function () {
$form = new Form();
$form->addText('foo')
->setRequired('fill foo');
$form->addText('bar')
->addConditionOn($form['foo'], Form::VALID)
->setRequired('fill bar');

$form->validate();
Assert::same(['fill foo'], $form->getErrors());

$form['foo']->setValue('abc');
$form->validate();
Assert::same(['fill bar'], $form->getErrors());

$form['bar']->setValue('abc');
$form->validate();
Assert::same([], $form->getErrors());
});


test(function () {

$form = new Form();

Assert::exception(function () use ($form) {
$form->addText('foo')
->addRule(Form::VALID);
}, 'Nette\InvalidArgumentException', 'You cannot use Form::VALID in the addRule method.');
});


test(function () {

$form = new Form();

Assert::exception(function () use ($form) {
$form->addText('foo')
->addCondition(Form::VALID);
}, 'Nette\InvalidArgumentException', 'You cannot use Form::VALID in the addCondition method.');
});


test(function () {

$form = new Form();

Assert::exception(function () use ($form) {
$form->addText('foo')
->addRule(~Form::VALID);
}, 'Nette\InvalidArgumentException', 'You cannot use Form::VALID in the addRule method.');
});

0 comments on commit cf8227a

Please sign in to comment.