mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Instance rules are not shared among other validator
objects (thus they are not static). This is useful
if you have a rule you do not want your other validators
to use (because it is very special to this validator).
The parameter given to addInstanceRule is identically to
addRule:
public function addInstanceRule(string $name, callable $callback [, string message])
Instead of appending to the static variables $_rules, and
$_ruleMessages, it now appends to $_instanceRules and
$_instanceRuleMessages.
A new getter for the rules and ruleMessage has also been added:
public array getRules()
public array getRuleMessages()
All existing code has been updated to use getRules(), and
getRuleMessages() instead of $_rules, and $_ruleMessages.
57 lines
1.1 KiB
PHP
57 lines
1.1 KiB
PHP
<?php
|
|
use Valitron\Validator;
|
|
|
|
class ValidateAddInstanceRuleTest extends BaseTestCase
|
|
{
|
|
protected function assertValid($v)
|
|
{
|
|
$msg = "\tErrors:\n";
|
|
$status = $v->validate();
|
|
foreach ($v->errors() as $label => $messages)
|
|
{
|
|
foreach ($messages as $theMessage)
|
|
{
|
|
$msg .= "\n\t{$label}: {$theMessage}";
|
|
}
|
|
}
|
|
|
|
$this->assertTrue($v->validate(), $msg);
|
|
}
|
|
|
|
public function testAddInstanceRule()
|
|
{
|
|
$v = new Validator(array(
|
|
"foo" => "bar",
|
|
"fuzz" => "bazz",
|
|
));
|
|
|
|
$v->addInstanceRule("fooRule", function($field, $value)
|
|
{
|
|
return $field !== "foo" || $value !== "barz";
|
|
});
|
|
|
|
Validator::addRule("fuzzerRule", function($field, $value)
|
|
{
|
|
return $field !== "fuzz" || $value === "bazz";
|
|
});
|
|
|
|
$v->rule("required", array("foo", "fuzz"));
|
|
$v->rule("fuzzerRule", "fuzz");
|
|
$v->rule("fooRule", "foo");
|
|
|
|
|
|
$this->assertValid($v);
|
|
}
|
|
|
|
public function testAddInstanceRuleFail()
|
|
{
|
|
$v = new Validator(array("foo" => "bar"));
|
|
$v->addInstanceRule("fooRule", function($field)
|
|
{
|
|
return $field === "for";
|
|
});
|
|
$v->rule("fooRule", "foo");
|
|
$this->assertFalse($v->validate());
|
|
}
|
|
}
|