加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:19:14 所属栏目:PHP教程 来源:网络整理
导读:如何使用 Respect Validation有多个自定义错误消息. 我有一些输入,我想针对多个验证器进行验证.我希望每个验证都有一个自定义错误消息. 这是我试过的: try { RespectValidationValidator::create() -key('foo',v::length(20)-setName('bar')-setTemplate(
如何使用 Respect Validation有多个自定义错误消息.

我有一些输入,我想针对多个验证器进行验证.我希望每个验证都有一个自定义错误消息.

这是我试过的:

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

输出是:

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

我希望它输出两个自定义错误消息.

Idealy我可以获得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 RespectValidationValidator as v;

try {
    $chained = RespectValidationValidator::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,));

} catch (RespectValidationExceptionsValidationException $exception) {
    $errors = $exception->findMessages(array(
        'bar',));
    var_dump($errors);
}

the print_r($chained) shows us:

RespectValidationValidator Object
(
    [rules:protected] => Array
        (
            [00000000791c0e000000000030f3f15e] => RespectValidationRulesKey Object
                (
                    [mandatory] => 1
                    [reference] => foo
                    [validator] => RespectValidationValidator Object
                        (
                            [rules:protected] => Array
                                (
                                    [00000000791c0e030000000030f3f15e] => RespectValidationRulesLength Object
                                        (
                                            [minValue] => 20
                                            [maxValue] =>
                                            [inclusive] => 1
                                            [name:protected] =>
                                            [template:protected] =>
                                        )

                                    [00000000791c0e020000000030f3f15e] => RespectValidationRulesAlnum 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 RespectValidationValidator 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) === 'RespectValidationRules'.$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) === 'RespectValidationRules'.$ruleType){
                $validators->rules[$key]->name = $name;
                return $validator;
            }
        }
    }

    public function setValidatorRuleTemplate($rulesetName,$template){
        $ruleset = $this->getRuleset($rulesetName);
        $validators = $ruleset->validator;
        foreach ($validators->rules as $key=>$validator){
            if (get_class($validator) === 'RespectValidationRules'.$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','Bubba's Custom Alnum!');
    $chained->setValidatorRuleName('foo','Length','bar');
    $chained->setValidatorRuleTemplate('foo','Bubba's Custom Length!');

    $chained->assert(array(
            'foo' => 'Hello,));
    var_dump($errors);
}

最终得到这个输出:

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

那很有趣!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读