2016-06-08 113 views
0

我一直在使用像這樣Laravel自定義錯誤消息不返回正確

public function boot() 
{ 
    $this->app['validator']->extend('googleUrl', function($attribute, $value, $parameters, $messages) 
    { 
     $url = $value; 
     $google_haystack = array('https://www.google.com', 'https://google.com'); 

      // Check the user's input against each array value 

      foreach ($google_haystack as $google_haystack) 
      { 
       if (strpos($url, $google_haystack) !== FALSE) 
       { 
        return TRUE; 
       } 
       return FALSE; 
      } 
    }); 
} 

規則的工作,因爲它應該服務供應商創造了一個新的自定義規則,但是當顯示錯誤消息,它只是顯示爲「驗證.google_url」。所以,在我的validation.php文件中,我已經定義了它,但它仍然只是返回以前的錯誤消息,而不是我的自定義消息。

/* 
|-------------------------------------------------------------------------- 
| Custom Validation Language Lines 
|-------------------------------------------------------------------------- 
| 
| Here you may specify custom validation messages for attributes using the 
| convention "attribute.rule" to name the lines. This makes it quick to 
| specify a specific custom language line for a given attribute rule. 
| 
*/ 

'custom' => [ 
    'attribute-name' => [ 
     'rule-name' => 'custom-message', 
    ], 
    'validation.google_url' => [ 
     'googleUrl' => 'You must enter a valid Google URL.', 
    ], 
], 

回答

1

的消息應該被放置在陣列的第一電平,而不是定製陣列,這是僅適用於特定屬性的錯誤消息內。

+0

謝謝!值得注意的是,即使我的規則名稱是googleUrl ... int validation.php文件,它要求我使用'google_url'作爲規則名稱......不知道爲什麼。 –