在 Laravel 4 中显示自定义验证的错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18250917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display Error Message for Custom Validation in Laravel 4
提问by Rooneyl
I have created a custom error function by creating a class;
我通过创建一个类创建了一个自定义错误函数;
<?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;
}
}
I reference it in my model;
我在我的模型中引用了它;
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'
);
It has been registered in my global.php;
它已在我的global.php 中注册;
Validator::resolver(function($translator, $data, $rules, $messages) {
return new CoreValidator($translator, $data, $rules, $messages);
});
It all works well, but the error message it returns is
一切正常,但它返回的错误消息是
validation.postcode
验证.邮政编码
How/where do I set a custom error message for this?
I have tried setting app/lang/en/validation.phpwith (neither work);
我如何/在哪里为此设置自定义错误消息?
我试过用(都不起作用)设置app/lang/en/validation.php;
'custom' => array(
"validation.postcode" => "my error message 1",
"postcode" => "my error message 2"
)
P.S. I know that there is a regex validation method already, but this problem is more generic for me.
PS 我知道已经有一个正则表达式验证方法,但是这个问题对我来说更通用。
回答by Rooneyl
I think I have cracked it.
我想我已经破解了它。
I added the message to the main array in app/lang/en/validation.php, not into the custom sub-array.
我将消息添加到app/lang/en/validation.php 中的主数组,而不是自定义子数组。
return array(
...
"url" => "The :attribute format is invalid.",
"postcode" => "my error message 2",
...
)
If this isn't the correct way, then someone is free to correct me.
如果这不是正确的方法,那么有人可以自由地纠正我。
回答by l3ehnam
You can use setCustomMessages()
method to assign custom messages like the bellow code
您可以使用setCustomMessages()
方法来分配自定义消息,如波纹管代码
<?php
class CoreValidator extends Illuminate\Validation\Validator
{
private $custom_messages = array(
"customvalidation" => "my error message.",
);
public function __construct($translator, $data, $rules, $messages = array(), $customAttributes = array())
{
parent::__construct($translator, $data, $rules, $messages, $customAttributes);
$this->setCustomMessages($this->custom_messages);
}
public function validateCustomvalidation($attribute, $value, $parameters = null)
{
// validation code here
}
}
回答by antoniputra
maybe this code more better :
也许这段代码更好:
// for example I am using sub-array custom at default validation file, but you can do it in other file as you wishes.
..other..
'custom' => array(
'email' => array(
'required' => 'We need to know your e-mail address!',
),
"required" => "Hey!!! don't forget at :attribute field is required.",
),
..other..
// you can determine your custom languages at your wishes file
$messages = \Lang::get('validation.custom');
Validator::make($input, $rules, $messages);
回答by brazorf
From documentation:
从文档:
In some cases, you may wish to specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the app/lang/xx/validation.php language file.
在某些情况下,您可能希望在语言文件中指定您的自定义消息,而不是将它们直接传递给验证器。为此,请将您的消息添加到 app/lang/xx/validation.php 语言文件中的自定义数组。
'custom' => array(
'email' => array(
'required' => 'We need to know your e-mail address!',
),
),
That means, in your case,
这意味着,在你的情况下,
'custom' => array(
'postcode' => array(
'PostCode' => 'error message for PostCode rule',
'required' => 'error message for required rule',
),
),
回答by Jon
If you want to utilize the custom validation messages array in app/lang/xx/validation.php, the correct way is as follows:
如果你想使用 app/lang/xx/validation.php 中的自定义验证消息数组,正确的方法如下:
'custom' => array(
'formFieldName' => array(
'postcode' => 'error message for PostCode rule',
'iamalwayslowercase' => 'error message for this rule'
),
),
Note that you use the name of the form field and then in the array you use the lowercased name of the rule.
请注意,您使用表单字段的名称,然后在数组中使用规则的小写名称。
回答by Mushangi Derrick
The code below also works perfectly, take note of the underscore on the index of the $customValidatorMessages
array. Hope it helps someone :-)
下面的代码也可以完美运行,请注意$customValidatorMessages
数组索引上的下划线。希望它可以帮助某人:-)
class CoreValidator extends Illuminate\Validation\Validator
{
/**
* The array of custom validator error messages.
*
* @var array
*/
protected $customValidatorMessages = array();
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;
}
$this->customValidatorMessages['post_code'] = 'Postcode error message.';
$this->setCustomMessages($this->customValidatorMessages);
return false;
}
}