2013-03-12 36 views
8

如何使用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) 

我預期輸出兩個自定義錯誤消息。

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) 

這個問題好像翻滾雜草。

+1

1.愚蠢的問題,但你的Respect版本多大年紀,因爲看起來每年都有'setTemplate()'函數的錯誤,「_當創建異常時,它會檢查模板在那裏,但它返回爲null_「https://github.com/Respect/Validation/issues/64。 2.我找不到這個功能的docco,它是否作爲公共API的一部分(因此,你知道這個_should_應該如你所期望的那樣工作嗎?)? 3.你是否嘗試過不同的驗證器?例如,它總是隻有最後一個添加其味精? – Sepster 2013-03-24 18:16:00

+0

4.出於興趣,您是否按照[Informative Excitedtions](http://documentup.com/Respect/Validation/#feature-guide/informative-exceptions)中的'getFullMessage();'得到了預期的輸出結果docco?這至少可以提供一個線索,看看它是否是丟失的異常信息,或者只是輸出異常的錯誤信息。 – Sepster 2013-03-24 19:06:15

+0

@Sepster 1.我正在使用最新的開發人員。 2。我通過查看代碼找到了這個函數,它是一個公共方法,所以我們可以假定它是公共API的一部分。 3.是的,它似乎總是鏈中的最後一個驗證者。 4.'getFullMessage()'輸出默認消息而不是自定義消息。 – Petah 2013-03-24 20:53:16

回答

3

您不能將它們鏈接在一起並獲取自定義消息,因爲您調用的上一個自定義消息將被簡單地分配給規則集,而不是由於實施鏈接而導致的單個規則。

爲了這個證明,我克隆它混帳,創造了一個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." } 

太有趣了!