自定義錯(cuò)誤信息

2018-02-24 15:52 更新

如果有需要,您可以設(shè)置自定義的錯(cuò)誤信息取代默認(rèn)錯(cuò)誤信息。這里有幾種方式可以設(shè)定自定義消息。
傳遞自定義消息進(jìn)驗(yàn)證器

$messages = [
    'required' => 'The :attribute field is required.',
];

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

注意: 在驗(yàn)證中,:attribute 占位符會(huì)被字段的實(shí)際名稱給取代。您也可以在驗(yàn)證信息中使用其他的占位符。

其他的驗(yàn)證占位符

$messages = [
    'same'    => 'The :attribute and :other must match.',
    'size'    => 'The :attribute must be exactly :size.',
    'between' => 'The :attribute must be between :min - :max.',
    'in'      => 'The :attribute must be one of the following types: :values',
];

為特定屬性賦予一個(gè)自定義信息

有時(shí)您只想為一個(gè)特定字段指定一個(gè)自定義錯(cuò)誤信息:

$messages = [
    'email.required' => 'We need to know your e-mail address!',
];

在語(yǔ)言包文件中指定自定義消息

某些狀況下,您可能希望在語(yǔ)言包文件中設(shè)定您的自定義消息,而非直接將他們傳遞給 Validator。要達(dá)到這個(gè)目的,將您的信息增加至 resources/lang/xx/validation.php 文件的 custom 數(shù)組中。

'custom' => [
    'email' => [
        'required' => 'We need to know your e-mail address!',
    ],
],
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)