php 方法验证不存在 - Laravel 5.4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45795508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:09:17  来源:igfitidea点击:

Method validate does not exist - Laravel 5.4

phplaravelvalidationlaravel-5laravel-5.4

提问by Parth Vora

I have a very weird problem. When I'm submitting the form, it throws an error with server-side validation.

我有一个非常奇怪的问题。当我提交表单时,它会引发服务器端验证错误。

Here is my simple controller:

这是我的简单控制器:

namespace App\Http\Controllers;

use Newsletter;
use Illuminate\Http\Request;

class SubscriptionController extends Controller
{
    public function subscribe(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            ]);
    }
}

Submitting the form gives me:

提交表格给了我:

BadMethodCallException Method validate does not exist.

BadMethodCallException 方法验证不存在。

it should work according to:

它应该按照以下方式工作:

https://laravel.com/docs/5.4/validation

https://laravel.com/docs/5.4/validation

回答by arku

In docs said:

在文档中说:

$this->validate($request, [
    'email' => 'required|email',
]);

This string - works :)

这个字符串 - 有效:)

回答by AddWeb Solution Pvt Ltd

You should try this:

你应该试试这个:

$validateFields = array('email' => 'required|email');

$this->validate($request, $validateFields);

OR

或者

$this->validate($request, [
    'email' => 'required|email'
]);

回答by Emeka Mbah

Well means its no longer available in 5.4 however its available in controller

好吧意味着它在 5.4 中不再可用,但它在控制器中可用

Try:

尝试:

 $this->validate($request, [
    'email' => 'required|email',
 ]);

回答by Vijay Trada

        $validator = \Validator::make($request->all(), [
            'mobile_number' => 'required',]);

        if ($validator->fails()) {
            return redirect()->back()
            ->withErrors($validator)
            ->withInput();
        }

Hope this works for you..

希望这对你有用..

回答by ferdousulhaque

Actually If you add the right controller, validate method should be already included. You can try adding below controller.

实际上,如果您添加了正确的控制器,则应该已经包含了验证方法。您可以尝试添加以下控制器。

Instead: use App\Http\Controllers\Controller;

反而: use App\Http\Controllers\Controller;

回答by Sonali Bhat

You can use the Validator service provider.

您可以使用验证器服务提供程序。

     namespace App\Http\Controllers;

     use Newsletter;
     use Illuminate\Http\Request;
     use Validator;

     class SubscriptionController extends Controller
     {
          public function subscribe(Request $request)
          {
                   $request->validate($request->all(),[
                     'email' => 'required|email',
                  ]);
           }

回答by Asad Ullah

lets add these two packages Best of luck

让我们添加这两个包祝你好运

use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers;

使用 Illuminate\Support\Facades\Validator; 使用 Illuminate\Foundation\Auth\RegistersUsers;