2013-03-17 72 views
3

當試圖實現Laravel的基於長度驗證在我看來Laravel:表單驗證字符串長度的錯誤信息導致的異常

'password' => array(
    'required', 
    'alpha_dash', 
    'Min:7' 
) 

和輸出錯誤信息

{{ 
    $errors->first(
     'password', 
     '<span class="error">:message</span>' 
    ) 
}} 

我得到

Unhandled Exception 
Message: 
Array to string conversion 
Location: 
_avalog\laravel\messages.php on line 188 
Stack Trace: 
#0 _avalog\laravel\laravel.php(42): Laravel\Error::native(8, 'Array to string...', '_avalog...', 188) 
#1 [internal function]: Laravel\{closure}(8, 'Array to string...', '_avalog...', 188, Array) 
#2 _avalog\laravel\messages.php(188): str_replace(':message', Array, 'get('password', 'get() 

調試,它似乎是真的。如果我print_r($validation);

Laravel\Validator Object (
    [attributes] => Array (
     [username] => fred 
     [email] => 
     [password] => asd 
     [csrf_token] => DWg3CUfqtMZkIRfyZXNEqygvWUHsGS9SQMue2V4S 
    ) 
    [errors] => Laravel\Messages Object (
     [messages] => Array (
      [email] => Array (
       [0] => The email field is required. 
      ) 
      [password] => Array ( 
       [0] => Array (
        [numeric] => The password must be at least 7. 
        [file] => The password must be at least 7 kilobytes. 
        [string] => The password must be at least 7 characters. 
       ) 
      ) 
    ) 
    [format] => :message 
) 

你可以看到消息事實上確實包含密碼的數組這似乎是取決於輸入類型,即使我在規則中所指定它是alphadash

[password] => Array (
    [0] => Array (
     [numeric] => The password must be at least 7. 
     [file] => The password must be at least 7 kilobytes. 
     [string] => The password must be at least 7 characters. 
    ) 
) 

儘管勞逸結合,不要

[email] => Array (
    [0] => The email format is invalid. 
) 

看着Laravel框架中的,它沒有任何處理這種基於數組的消息傳遞,所以我認爲我在做到錯誤之前做了些什麼,但我不知道是什麼。

感謝您的幫助。

回答

11

您的規則條目是錯誤的。它必須像

'password' => 'required|alpha_dash|min:7' 

看那Laravel驗證docs瞭解更多信息

+1

哇,現在我覺得愚蠢。我使用了pipe分隔的語法_several_ times無濟於事,但似乎Min的大寫M是原因。感謝您的幫助。這是一個恥辱,這些文件沒有提到任何規則*必須*小寫。 – domwrap 2013-03-18 03:18:42

+0

很高興我幫助:)標記問題,如果它是如此:) – 2013-03-19 00:35:13

+1

@Hwulex不要擔心,這不是你,我的團隊中的每個人都有同樣的問題,Laravel文檔真的很糟糕,當涉及到這個! – Sliq 2016-04-05 12:40:42