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

前端之家收集整理的这篇文章主要介绍了php – 尊重验证多个自定义错误消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用 Respect Validation有多个自定义错误消息.

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

这是我试过的:

  1. try {
  2. Respect\Validation\Validator::create()
  3. ->key('foo',v::length(20)->setName('bar')->setTemplate('Custom length message.')
  4. ->alnum()->setName('baz')->setTemplate('Custom alnum message.')
  5. )
  6. ->assert([
  7. 'foo' => 'Hello,world!',]);
  8. } catch (Respect\Validation\Exceptions\ValidationException $exception) {
  9. $errors = $exception->findMessages([
  10. 'bar','baz',]);
  11. var_dump($errors);
  12. }

输出是:

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

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

Idealy我可以获得1个输入的消息数组,如:

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

会给我:

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

这个问题看起来像是杂草.

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

为了证明这一点,我从git克隆了它,创建了一个bin目录,并用这个test.PHP稍微修改了你的样本

  1. <?PHP
  2. set_include_path(implode(PATH_SEPARATOR,array(
  3. realpath('../library')
  4. )));
  5.  
  6. function __autoload($class_name) {
  7. include $class_name . '.PHP';
  8. }
  9.  
  10. use Respect\Validation\Validator as v;
  11.  
  12. try {
  13. $chained = Respect\Validation\Validator::create()
  14. ->key('foo',v::length(20)->setName('bar')->setTemplate('Custom length message.')
  15. ->alnum()->setName('baz')->setTemplate('Custom alnum message.')
  16. );
  17.  
  18. print_r($chained);
  19.  
  20. $chained->assert(array(
  21. 'foo' => 'Hello,));
  22.  
  23. } catch (Respect\Validation\Exceptions\ValidationException $exception) {
  24. $errors = $exception->findMessages(array(
  25. 'bar',));
  26. var_dump($errors);
  27. }
  28.  
  29. the print_r($chained) shows us:
  30.  
  31. Respect\Validation\Validator Object
  32. (
  33. [rules:protected] => Array
  34. (
  35. [00000000791c0e000000000030f3f15e] => Respect\Validation\Rules\Key Object
  36. (
  37. [mandatory] => 1
  38. [reference] => foo
  39. [validator] => Respect\Validation\Validator Object
  40. (
  41. [rules:protected] => Array
  42. (
  43. [00000000791c0e030000000030f3f15e] => Respect\Validation\Rules\Length Object
  44. (
  45. [minValue] => 20
  46. [maxValue] =>
  47. [inclusive] => 1
  48. [name:protected] =>
  49. [template:protected] =>
  50. )
  51.  
  52. [00000000791c0e020000000030f3f15e] => Respect\Validation\Rules\Alnum Object
  53. (
  54. [additionalChars] =>
  55. [stringFormat] => /^(\s|[a-zA-Z0-9])*$/
  56. [name:protected] =>
  57. [template:protected] =>
  58. )
  59.  
  60. )
  61.  
  62. [name:protected] => baz
  63. [template:protected] => Custom alnum message.
  64. )
  65.  
  66. [name:protected] => foo
  67. [template:protected] =>
  68. )
  69.  
  70. )
  71.  
  72. [name:protected] =>
  73. [template:protected] =>
  74. )

您可能会注意到规则集选取了姓氏以及传入的最后一个模板,并且两个实际验证对象都没有获取名称或模板.我没有在图书馆看到任何方法来实际做你想做的事情.

所以我决定采取行动.在我的../bin目录中,我创建了这个类,扩展了Valditor类.

  1. <?PHP
  2. use Respect\Validation\Validator as v;
  3. class BubbaValidator extends v {
  4.  
  5. public function getRuleset($rulename = null){
  6. if (is_null($rulename)) return $this->rules;
  7. foreach ($this->rules as $rule){
  8. if ($rule->getName() == $rulename){
  9. return $rule;
  10. }
  11. }
  12. }
  13.  
  14. public function getValidatorRules($rulesetName,$ruleType=null){
  15. $ruleset = $this->getRuleset($rulesetName);
  16. $validators = $ruleset->validator;
  17.  
  18. if (is_null($ruleType)){
  19. return $validators;
  20. }
  21.  
  22. foreach ($validators->rules as $key=>$validator){
  23. if (get_class($validator) === 'Respect\Validation\Rules\\'.$ruleType){
  24. $validator->name = "bar";
  25. $validator->template = "bubba rocks";
  26. $validators->rules[$key]->name = "bar";
  27. $validators->rules[$key]->template = "bubba rocks";
  28. return $validator;
  29. }
  30. }
  31. }
  32.  
  33. public function setValidatorRuleName($rulesetName,$ruleType,$name){
  34. $ruleset = $this->getRuleset($rulesetName);
  35. $validators = $ruleset->validator;
  36. foreach ($validators->rules as $key=>$validator){
  37. if (get_class($validator) === 'Respect\Validation\Rules\\'.$ruleType){
  38. $validators->rules[$key]->name = $name;
  39. return $validator;
  40. }
  41. }
  42. }
  43.  
  44. public function setValidatorRuleTemplate($rulesetName,$template){
  45. $ruleset = $this->getRuleset($rulesetName);
  46. $validators = $ruleset->validator;
  47. foreach ($validators->rules as $key=>$validator){
  48. if (get_class($validator) === 'Respect\Validation\Rules\\'.$ruleType){
  49. $validators->rules[$key]->template = $template;
  50. return $validator;
  51. }
  52. }
  53. }
  54.  
  55. }

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

  1. <?PHP
  2. set_include_path(implode(PATH_SEPARATOR,array(
  3. realpath('../library'),realpath(__DIR__)
  4. )));
  5.  
  6. function __autoload($class_name) {
  7. include $class_name . '.PHP';
  8. }
  9.  
  10. use BubbaValidator as v;
  11.  
  12. try {
  13. $chained = new BubbaValidator();
  14. $chained->key('foo',v::length(20)->setName('bar')->setTemplate('Custom length message.')
  15. ->alnum()->setName('baz')->setTemplate('Custom alnum message.')
  16. );
  17.  
  18. $chained->setValidatorRuleName('foo','Alnum','baz');
  19. $chained->setValidatorRuleTemplate('foo','Bubba\'s Custom Alnum!');
  20. $chained->setValidatorRuleName('foo','Length','bar');
  21. $chained->setValidatorRuleTemplate('foo','Bubba\'s Custom Length!');
  22.  
  23. $chained->assert(array(
  24. 'foo' => 'Hello,));
  25. var_dump($errors);
  26. }

最终得到这个输出

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

那很有趣!

猜你在找的PHP相关文章