gpt4 book ai didi

php - 尊重验证多个自定义错误消息

转载 作者:可可西里 更新时间:2023-11-01 12:38:16 31 4
gpt4 key购买 nike

如何使用 Respect Validation 显示多个自定义错误消息? .

我有一些输入要针对多个验证器进行验证。我希望每次验证都有一条自定义错误消息。

这是我尝试过的:

try {
Respect\Validation\Validator::create()
->key('foo',
v::length(20)->setName('bar')->setTemplate('Custom length message.')
->alnum()->setName('baz')->setTemplate('Custom alnum message.')
)
->assert([
'foo' => 'Hello, world!',
]);
} catch (Respect\Validation\Exceptions\ValidationException $exception) {
$errors = $exception->findMessages([
'bar',
'baz',
]);
var_dump($errors);
}

输出是:

array (size=2)
'bar' => string '' (length=0)
'baz' => string 'Custom alnum message.' (length=21)

我预计它会输出两条自定义错误消息。

理想情况下,我可以为 1 个输入获取一组消息,例如:

var_dump($exception->findMessages(['foo']));

会给我:

array (size=1)
'foo' =>
array (size=2)
0 => string 'Custom length message.' (length=22)
1 => string 'Custom alnum message.' (length=21)

这个问题看起来像风滚草。

最佳答案

您不能将它们链接在一起并获得自定义消息,因为您调用的最后一个自定义消息将简单地分配给规则集,而不是由于链接的实现而分配给单独的规则。

为了演示这一点,我从 git 中克隆了它,创建了一个 bin 目录,并使用这个 test.php 稍微修改了您的示例

<?php
set_include_path(implode(PATH_SEPARATOR, array(
realpath('../library')
)));

function __autoload($class_name) {
include $class_name . '.php';
}

use Respect\Validation\Validator as v;

try {
$chained = Respect\Validation\Validator::create()
->key('foo',
v::length(20)->setName('bar')->setTemplate('Custom length message.')
->alnum()->setName('baz')->setTemplate('Custom alnum message.')
);

print_r($chained);

$chained->assert(array(
'foo' => 'Hello, world!',
));

} catch (Respect\Validation\Exceptions\ValidationException $exception) {
$errors = $exception->findMessages(array(
'bar',
'baz',
));
var_dump($errors);
}

the print_r($chained) shows us:

Respect\Validation\Validator Object
(
[rules:protected] => Array
(
[00000000791c0e000000000030f3f15e] => Respect\Validation\Rules\Key Object
(
[mandatory] => 1
[reference] => foo
[validator] => Respect\Validation\Validator Object
(
[rules:protected] => Array
(
[00000000791c0e030000000030f3f15e] => Respect\Validation\Rules\Length Object
(
[minValue] => 20
[maxValue] =>
[inclusive] => 1
[name:protected] =>
[template:protected] =>
)

[00000000791c0e020000000030f3f15e] => Respect\Validation\Rules\Alnum Object
(
[additionalChars] =>
[stringFormat] => /^(\s|[a-zA-Z0-9])*$/
[name:protected] =>
[template:protected] =>
)

)

[name:protected] => baz
[template:protected] => Custom alnum message.
)

[name:protected] => foo
[template:protected] =>
)

)

[name:protected] =>
[template:protected] =>
)

您可能会注意到,规则集选取了姓氏以及传入的最后一个模板,并且实际验证对象都没有获得名称或模板。我在库中看不到任何方法可以实际执行您尝试执行的操作。

所以我决定另辟蹊径。在我的 ../bin 目录中,我创建了这个类,扩展了 Valditor 类。

<?php
use Respect\Validation\Validator as v;
class BubbaValidator extends v {

public function getRuleset($rulename = null){
if (is_null($rulename)) return $this->rules;
foreach ($this->rules as $rule){
if ($rule->getName() == $rulename){
return $rule;
}
}
}

public function getValidatorRules($rulesetName, $ruleType=null){
$ruleset = $this->getRuleset($rulesetName);
$validators = $ruleset->validator;

if (is_null($ruleType)){
return $validators;
}

foreach ($validators->rules as $key=>$validator){
if (get_class($validator) === 'Respect\Validation\Rules\\'.$ruleType){
$validator->name = "bar";
$validator->template = "bubba rocks";
$validators->rules[$key]->name = "bar";
$validators->rules[$key]->template = "bubba rocks";
return $validator;
}
}
}

public function setValidatorRuleName($rulesetName, $ruleType, $name){
$ruleset = $this->getRuleset($rulesetName);
$validators = $ruleset->validator;
foreach ($validators->rules as $key=>$validator){
if (get_class($validator) === 'Respect\Validation\Rules\\'.$ruleType){
$validators->rules[$key]->name = $name;
return $validator;
}
}
}

public function setValidatorRuleTemplate($rulesetName, $ruleType, $template){
$ruleset = $this->getRuleset($rulesetName);
$validators = $ruleset->validator;
foreach ($validators->rules as $key=>$validator){
if (get_class($validator) === 'Respect\Validation\Rules\\'.$ruleType){
$validators->rules[$key]->template = $template;
return $validator;
}
}
}

}

然后我修改了脚本并运行了它

<?php
set_include_path(implode(PATH_SEPARATOR, array(
realpath('../library'),
realpath(__DIR__)
)));

function __autoload($class_name) {
include $class_name . '.php';
}

use BubbaValidator as v;

try {
$chained = new BubbaValidator();
$chained->key('foo',
v::length(20)->setName('bar')->setTemplate('Custom length message.')
->alnum()->setName('baz')->setTemplate('Custom alnum message.')
);

$chained->setValidatorRuleName('foo', 'Alnum', 'baz');
$chained->setValidatorRuleTemplate('foo', 'Alnum', 'Bubba\'s Custom Alnum!');
$chained->setValidatorRuleName('foo', 'Length', 'bar');
$chained->setValidatorRuleTemplate('foo', 'Length', 'Bubba\'s Custom Length!');

$chained->assert(array(
'foo' => 'Hello, world!',
));

} catch (Respect\Validation\Exceptions\ValidationException $exception) {
$errors = $exception->findMessages(array(
'bar',
'baz',
));
var_dump($errors);
}

最终得到这个输出:

  D:\Users\Bubba\git\Validation\bin>php test.php 
array(2) {
["bar"]=>
string(22) "Bubba's Custom Length!"
["baz"]=>
string(21) "Custom alnum message." }

那很有趣!

关于php - 尊重验证多个自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15351821/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com