Laravel无法在忘记密码控制器中使用表单请求验证

时间:2017-11-25 08:16:13

标签: php laravel validation laravel-5.5

在具有以下验证方法的ForgotPasswordController控制器中,

protected function validateEmail(Request $request)
    {  
        $this->validate($request, [
            'email' => 'required|email|exists:users'
        ]);
    }

以上工作正常,但我想用表单请求验证进行验证,所以这里是下面的代码

    <?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use App\Http\Requests\AdminForgotPasswordEmailValidation;
class ForgotPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset emails and
    | includes a trait which assists in sending these notifications from
    | your application to your users. Feel free to explore this trait.
    |
    */

    use SendsPasswordResetEmails;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest:admin');
    }

    /**
     * Display the form to request a password reset link.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLinkRequestForm()
    {
        return view('admin.passwords.email');
    }

    public function broker()
    {
        return Password::broker('admins');
    }

    protected function validateEmail(AdminForgotPasswordEmailValidation $request)
    {

        //nothing here

    }

}

有一个奇怪的错误,

Type error: Argument 1 passed to App\Http\Controllers\Admin\ForgotPasswordController::validateEmail() must be an instance of App\Http\Requests\AdminForgotPasswordEmailValidation, instance of Illuminate\Http\Request given, called in /home/**/public_html/project/vendor/laravel/framework/src/Illuminate/Foundation/Auth/SendsPasswordResetEmails.php on line 28

此表单请求验证在其他表单中正常运行

2 个答案:

答案 0 :(得分:0)

在课程之前添加此行

use App\Http\Requests\AdminForgotPasswordEmailValidation;

答案 1 :(得分:0)

这不是路由器调度的方法,因此在该方法上不会发生依赖注入。 sendResetLinkEmail是路由指向的内容,它调用validateEmail($request)

php artisan route:list

POST      | password/email                 | password.email     | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail

覆盖sendResetLinkEmail上的ForgotPasswordController,不要拨打$this->validateEmail($request)并让签名为您的请求类提供类型提示。

public function sendResetLinkEmail(AdminForgotPasswordEmailValidation $request)

这将解决您的表单请求,从而导致其验证。