我正在使用默认的Laravel 5用户注册内容,该内容使用registrar.php文件app / Services / Registrar.php
我需要做一个自定义验证规则来检查会话中是否存储了一些东西,否则用户就无法注册。
我已经尝试创建自己的验证规则,但似乎没有被调用。我做错了什么?
<?php namespace App\Services;
use App\User;
use Event;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
use App\Events\UserRegistered;
use Session;
class Registrar implements RegistrarContract {
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
public function validator(array $data)
{
Validator::extend('required_images', function($attribute, $value, $parameters)
{
die("rule called");
});
return Validator::make($data, [
'first_name' => 'required_images|required|max:255',
'last_name' => 'required|max:255',
'discount_code' => 'max:255',
'register_email' => 'required|email|confirmed|max:255|unique:users,email',
'register_password' => 'required|confirmed|min:6|max:60'
]);
}
}
此外,我刚刚将规则required_images附加到first_name字段,但由于它实际上与注册表单中的字段无关,是否有更好的方法来触发它?
答案 0 :(得分:2)
如果仔细查看validator()
功能,可以看到实际上有return
声明:
public function validator(array $data)
{
return Validator::make($data, [
// ^^^
'first_name' => 'required_images|required|max:255',
'last_name' => 'required|max:255',
// ...
这意味着您的规则永远不会被注册。你应该在之前做到:
public function validator(array $data)
{
Validator::extend('required_images', function($attribute, $value, $parameters)
{
die("rule called");
});
return Validator::make($data, [
'first_name' => 'required_images|required|max:255',
'last_name' => 'required|max:255',
'discount_code' => 'max:255',
'register_email' => 'required|email|confirmed|max:255|unique:users,email',
'register_password' => 'required|confirmed|min:6|max:60'
]);
}