2013-07-15 13 views

回答

8

The Laravel 4 docs specifically state您需要爲自定義規則定義錯誤消息。

你有兩個選擇;

選項1:

$messages = array(
    'foo' => 'The :attribute field is foo.', 
); 

$validator = Validator::make($input, $rules, $messages); 

選項2:

指定的語言文件,而不是直接將它們傳遞給確認您的自定義消息。要做到這一點,在應用程序添加郵件到自定義陣列/郎/ XX/validation.php語言文件:

'custom' => array(
    'foo' => array(
     'required' => 'We need to know your foo!', 
    ), 
), 
+0

謝謝,看起來我完全在瀏覽驗證時錯過了該部分 延期。感謝你的幫助。 –

+0

我對「自定義」解決方案懷疑。這是不是意味着你必須爲每個字段指定消息,而不是你的自定義驗證器的所有情況?在這種情況下,'custom.foo'可能被視爲字段名稱,而不是您的'foo'驗證器。此外,文檔還說''custom'只能用於特定字段的自定義錯誤消息,例如'myField.required =>'message'''而不是''myValidator =>'message''。所以,我仍然在尋找解決方案,爲什麼我的自定義驗證器不會從validation.php文件中選擇消息。我想是時候進行調試了。也許L5改變了它? – JustAMartin

0

除了什麼TheShiftExchange說,如果你在一個validation.php語言文件看你會看到您可以指定的所有不同規則。因此,舉例來說,如果您的驗證有這樣的條目:

class ArticleValidator extends Validator 
{ 
    public static $rules = [ 
     'create' => [ 
      'title'      => ['required'], 
      'slug'      => ['required', 'regex:([a-z\0-9\-]*)'] 
     ] 
    ]; 

} 

然後你自定義的驗證規則可能是這樣的:

'custom' => array(
    'company_article_type_id' => array(
     'required' => 'The slug field is really important', 
     'exists' => 'The slug already exists', 
    ), 
), 

注意如何在「必要」,並在自定義的「存在」鍵驗證規則與上面驗證器中的驗證規則相匹配。

+0

這些規則特定於'company_article_type_id'字段。但是,如果我想在validation.php文件中爲我的完全自定義驗證函數的所有情況定義消息呢?出於某種原因,Laravel 5沒有拿起我的信息。 – JustAMartin

0

如果有人想知道Laravel 5:只需將所有默認消息下的消息添加到validation.php即可。例如:

​​

其中date_not_in_future是您的自定義功能validateDateNotInFuture。 Laravel每次在任何領域使用規則時都會選擇該消息,除非您想覆蓋特定字段的全局消息,否則不必使用custom數組。

完整的代碼來實現驗證器如下。

自定義驗證(與DATE_FORMAT獎金疑難雜症的意見和date_before本地化):

<?php namespace App\Services\Validation; 

use Illuminate\Validation\Validator as BaseValidator; 

/** 
* Class for your custom validation functions 
*/ 
class Validator extends BaseValidator { 

    public function validateDateNotInFuture($attribute, $value, $parameters) 
    { 
     // you could also test if the string is a date at all 
     // and if it matches your app specific format 
     // calling $this->validateDateFormat validator with your app's format 
     // loaded from \Config::get, but be careful - 
     // Laravel has hard-coded checks for DateFormat rule 
     // to extract correct format from it if it exists, 
     // and then use for validateBefore. If you have some unusual format 
     // and date_format has not been applied to the field, 
     // then validateBefore will give unpredictable results. 
     // Your best bet then is to override protected function 
     // getDateFormat($attribute) to return your app specific format 

     $tomorrow = date('your app date format here', strtotime("tomorrow")); 

     $parameters[0] = $tomorrow; 
     return $this->validateBefore($attribute, $value, $parameters); 
    } 
} 

ValidatorServiceProvider文件:

<?php namespace App\Providers; 

namespace App\Providers; 

use App\Services\Validation\Validator; 
use Illuminate\Support\ServiceProvider; 

class ValidatorServiceProvider extends ServiceProvider{ 

    public function boot() 
    { 
     \Validator::resolver(function($translator, $data, $rules, $messages) 
     { 
      return new Validator($translator, $data, $rules, $messages); 
     }); 
    } 

    public function register() 
    { 
    } 
} 

然後只需添加一行配置/ app.php:

'App\Providers\RouteServiceProvider', 
    'App\Providers\ValidatorServiceProvider', // your custom validation 
相關問題