我通过创建一个类创建了一个自定义错误函数;
<?php
class CoreValidator extends Illuminate\Validation\Validator
{
public function validatePostcode($attribute, $value, $parameters = null)
{
$regex = "/^((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))$/i";
if(preg_match($regex ,$value)) {
return true;
}
return false;
}
}
我在我的模型中引用它;
public static $rules = array(
'first_name' => 'required|Max:45',
'surname' => 'required|Max:45',
'address_line_1' => 'required|Max:255',
'address_line_2' => 'Max:255',
'address_line_3' => 'Max:255',
'town' => 'required|Max:45',
'county' => 'Max:45',
'postcode' => 'required|Postcode',
'phone_number' => 'required|Max:22'
);
它已在我的global.php中注册;
Validator::resolver(function($translator, $data, $rules, $messages) {
return new CoreValidator($translator, $data, $rules, $messages);
});
一切正常,但它返回的错误消息是
验证.邮政编码
如何/在哪里为此设置自定义错误消息?
我尝试过设置app/lang/en/validation.php(均无效);
'custom' => array(
"validation.postcode" => "my error message 1",
"postcode" => "my error message 2"
)
PS我知道已经有一个正则表达式验证方法,但这个问题对我来说更普遍。