未找到验证方法 - Laravel

时间:2017-09-11 22:57:10

标签: laravel

Laravel Framework 5.4.35

通讯录控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Routing\Redirector;

use Illuminate\Support\Facades\Mail;

use App\Mail\ContactEmail;

class ContactsController extends Controller
{
    public function index() {
        return view('contact.index');
    }

    public function sendContact (Request $request) {

        $request->validate([
            'name' => 'required|min:3',
            'email' => 'required|email',
            'message' => 'required|min:5',
        ]);

        Mail::to('bump@bumpy.net')
                ->send(new ContactEmail($request));

        return redirect('/contact/success');
    }

    public function success() {
        return view('contact.success');
    }
}

扩展的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

当它到达时:

$request->validate([
                'name' => 'required|min:3',
                'email' => 'required|email',
                'message' => 'required|min:5',
            ]);

我得到了这个输出:

  

(1/1)BadMethodCallException方法验证不存在

我有命名空间,要使用的类。对该方法的调用似乎没问题。

我错过了什么?

注意建议吗?

如果我使用Validator facade手动创建验证器实例。 它似乎验证了。

1 个答案:

答案 0 :(得分:2)

您提到使用的是5.4版本。您通过请求用于验证的方法仅来自版本5.5。

所以你需要这样做......

$this->validate($request, [
    'name' => 'required|min:3',
            'email' => 'required|email',
            'message' => 'required|min:5',
]);

希望这会有所帮助。查看5.4v文档而不是aster / 5.5v https://laravel.com/docs/5.4/validation#validation-quickstart

Laravel 5.4

$this->validate($request, [

Laravel 5.5

$request->validate([