2016-06-08 114 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'作为规则名称......不知道为什么。 –