我正在验证用户注册表单
表示EX: (Requests validation class)
class UserCreateAccountRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'email' => 'required|unique:users,email',
'password' => 'required|min:6|max:32'
];
}
}
如果电子邮件已注册,我想重定向到密码重置页面。
如何在不将验证放入控制器的情况下使用请求验证类来实现此目的?
答案 0 :(得分:1)
在授权功能内检查电子邮件是否已存在,
public function authorize()
{
$email = Request::input('email');
$result = User::where('email',$email)
->exists();
if($result)
{
return false;
}
return true;
}
如果返回false,则会触发forbiddenResponse函数,因此您需要包含该函数,并且可以在其中重定向到您想要的页面。如果电子邮件已存在,此函数将仅返回false。
public function forbiddenResponse()
{
return redirect('password_reset');
}
这就是全部。以下是Request类的结构,
<?php namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Response;
class FriendFormRequest extends FormRequest
{
public function rules()
{
return [
'first_name' => 'required',
'email_address' => 'required|email'
];
}
public function authorize()
{
// Only allow logged in users
// return \Auth::check();
// Allows all users in
return true;
}
// OPTIONAL OVERRIDE
public function forbiddenResponse()
{
// Optionally, send a custom response on authorize failure
// (default is to just redirect to initial page with errors)
//
// Can return a response, a view, a redirect, or whatever else
return Response::make('Permission denied foo!', 403);
}
// OPTIONAL OVERRIDE
public function response()
{
// If you want to customize what happens on a failed validation,
// override this method.
// See what it does natively here:
// https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Http/FormRequest.php
}
}