需要自定义密钥才能在 Laravel 5 中注册
Require custom key to register in Laravel 5
我正在处理的当前应用程序要求用户只有在获得注册码后才能进行注册。在 Laravel 5 中,我可以将其放入验证器中:
return Validator::make($data, [
// some validation fields...
'registration_key' => 'required|same:'.$registration_key
]);
这将要求 registration_key
字段与变量 $registration_key
相同。但是,输入错误的键后,视图的错误消息会显示键:
错误:注册密钥和[密钥的值]必须匹配。
显然,我不想向用户显示密钥。我知道 Laravel 有一些文档来展示如何扩展 Validator facade,但它解释得很差,我无法理解。有没有人做过类似的事情可以帮助我?
Validator::make($data, [
// some validation fields...
'registration_key' => 'required|same:' . $registration_key
], [
'registration_key.same' => 'your text for the error message'
]);
我不知道你是否可以在请求中声明你自己的错误信息Class但是你可以在控制器中检查它然后添加错误信息。
更多信息请查看:http://laravel.com/docs/5.1/validation#custom-error-messages
更新:
感谢 Josh Janusch 提供更简单的解决方案
class MyRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$registration_key = 'your key';
return [
'registration_key' => 'required|same:'.$registration_key
];
}
/**
* Set custom messages for validator errors.
*
* @return array
*/
public function messages()
{
return ['registration_key.same' => 'your key is not listed'];
}
}
我正在处理的当前应用程序要求用户只有在获得注册码后才能进行注册。在 Laravel 5 中,我可以将其放入验证器中:
return Validator::make($data, [
// some validation fields...
'registration_key' => 'required|same:'.$registration_key
]);
这将要求 registration_key
字段与变量 $registration_key
相同。但是,输入错误的键后,视图的错误消息会显示键:
错误:注册密钥和[密钥的值]必须匹配。
显然,我不想向用户显示密钥。我知道 Laravel 有一些文档来展示如何扩展 Validator facade,但它解释得很差,我无法理解。有没有人做过类似的事情可以帮助我?
Validator::make($data, [
// some validation fields...
'registration_key' => 'required|same:' . $registration_key
], [
'registration_key.same' => 'your text for the error message'
]);
我不知道你是否可以在请求中声明你自己的错误信息Class但是你可以在控制器中检查它然后添加错误信息。
更多信息请查看:http://laravel.com/docs/5.1/validation#custom-error-messages
更新:
感谢 Josh Janusch 提供更简单的解决方案
class MyRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$registration_key = 'your key';
return [
'registration_key' => 'required|same:'.$registration_key
];
}
/**
* Set custom messages for validator errors.
*
* @return array
*/
public function messages()
{
return ['registration_key.same' => 'your key is not listed'];
}
}