mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Validator::rule now accepts any kind of callback
This has been achieved by making sure that the rule is neither
a "native" Validator rule (e.g. there is a validate{$CamleCaseName}
method) nor it has been registered.
If both conditions are met, the rule (even if it is a string)
will be accepted and used.
Tests for this case have also been added.
This commit is contained in:
parent
ebbeca46af
commit
a6be7e2dd0
@ -1066,6 +1066,20 @@ class Validator
|
|||||||
return $name;
|
return $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if either a valdiator with the given name has been
|
||||||
|
* registered or there is a default validator by that name.
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasValidator($name)
|
||||||
|
{
|
||||||
|
$rules = $this->getRules();
|
||||||
|
return method_exists($this, "validate" . ucfirst($name))
|
||||||
|
|| isset($rules[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convenience method to add a single validation rule
|
* Convenience method to add a single validation rule
|
||||||
*
|
*
|
||||||
@ -1079,12 +1093,8 @@ class Validator
|
|||||||
// Get any other arguments passed to function
|
// Get any other arguments passed to function
|
||||||
$params = array_slice(func_get_args(), 2);
|
$params = array_slice(func_get_args(), 2);
|
||||||
|
|
||||||
// Note: we cannot use is_callable here since max, int, and many
|
if (is_callable($rule)
|
||||||
// other string can also be callables but aren't really rule callbacks.
|
&& !(is_string($rule) && $this->hasValidator($rule)))
|
||||||
//
|
|
||||||
// If a closure is used, we can be sure that the user actually
|
|
||||||
// wants $rule to be a custom rule check.
|
|
||||||
if (is_object($rule) && ($rule instanceof \Closure))
|
|
||||||
{
|
{
|
||||||
$name = $this->getUniqueRuleName($fields);
|
$name = $this->getUniqueRuleName($fields);
|
||||||
$msg = isset($params[0]) ? $params[0] : null;
|
$msg = isset($params[0]) ? $params[0] : null;
|
||||||
|
|||||||
@ -1,6 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
use Valitron\Validator;
|
use Valitron\Validator;
|
||||||
|
|
||||||
|
function callbackTestFunction($item, $value)
|
||||||
|
{
|
||||||
|
return $value === "bar";
|
||||||
|
}
|
||||||
|
|
||||||
class ValidateAddInstanceRuleTest extends BaseTestCase
|
class ValidateAddInstanceRuleTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
protected function assertValid($v)
|
protected function assertValid($v)
|
||||||
@ -87,6 +92,20 @@ class ValidateAddInstanceRuleTest extends BaseTestCase
|
|||||||
$this->assertEquals("Foo test error message", $errors["foo"][0]);
|
$this->assertEquals("Foo test error message", $errors["foo"][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAddRuleWithNamedCallbackOk()
|
||||||
|
{
|
||||||
|
$v = new Validator(array("bar" => "foo"));
|
||||||
|
$v->rule("callbackTestFunction", "bar");
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddRuleWithNamedCallbackErr()
|
||||||
|
{
|
||||||
|
$v = new Validator(array("foo" => "bar"));
|
||||||
|
$v->rule("callbackTestFunction", "foo");
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
public function testUniqueRuleName()
|
public function testUniqueRuleName()
|
||||||
{
|
{
|
||||||
$v = new Validator(array());
|
$v = new Validator(array());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user