【问题标题】:Laravel: ValidationLaravel:验证
【发布时间】:2020-08-14 21:30:58
【问题描述】:

我正在尝试使用 laravel 的验证器,但随后它显示错误消息“找不到类 'App\Http\Controllers\Validator'”。这真的是因为我必须制作验证器类吗?如果是这样,那我该怎么做呢?请帮忙。这是我的代码。

web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
   return view('login');
});

Route::post('/home', 'loginController@doLogin');

登录控制器.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class loginController extends Controller
{
  //
  public function doLogin(){
    $rules=array(
        'email' => 'required|email',
        'password' => 'required|alphaNum|min:3'
    );

    $validator=Validator::make(Input::all(), $rules);
    if($validator->fails()){
        return Redirect::to('login')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    }else{
        $userdata=array(
            'email' => Input::get('login-email'),
            'password' => Input::get('login-password')
        );
        return view('halamanUtama');
    }
  }

}

login.blade.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Login</title>

    <!--CSS-->
    <link rel="stylesheet" href="{{ asset('css/styles.css') }}" type="text/css">
 </head>
 <body>
   <div class="login-page-frame">
     <div class="header-login-page-frame">
        <h3>Login</h3>
     </div>
     <div class="inner-login-form-frame">
        <form method="post" action="./home" class="login-form">
            {{ csrf_field() }}
            <input type="text" placeholder="email" name="login-email">
            <br>
            <input type="password" placeholder="pass" name="login-password">
            <br>
            <button type="submit" class="btn-login">Log In</button>
        </form>
     </div>
   </div>
 </body>

【问题讨论】:

    标签: php laravel validation authentication


    【解决方案1】:

    对于表单验证,您可以使用请求,您可以为每个表单进行此操作并设置特定规则。

    更多信息:" https://laravel.com/docs/7.x/validation#form-request-validation

    例子:

    class StoreUserRequest extends FormRequest
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return Auth::check();
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                'name' => 'required|string|max:255',
                'email' => 'required|string|email|max:255|unique:users',
            ];
        }
    }
    

    【讨论】:

    • 我试图阅读它,但我无法理解它。那么,如果我使用这个,是不是意味着我只需要在 web.php 上的 post 方法就可以直接指向 StoreUserRequest.authorize 吗?
    • 不,你让 web.php 中的 post 方法仍然引用相同的函数。但是您可以使用 StoreUserRequest 参数来代替 Request 参数。这将验证所有字段。字段验证将添加到规则函数中的返回数组中。
    • 好吧,对不起,我没有读过关于请求的任何内容,所以我在理解上有些困难,这意味着你的方法是首先使用 php artisan 创建表单请求,然后使代码像你向我展示了,在我的 doLogin 函数中,我给出了参数 $request,但不是使用 Request,而是使用 StoreUserRequest,是这样吗?另外,如您所见,我的 post 方法路线仅显示 /home。它会自动接受我的输入吗?
    • 从 doLogin 函数中移除验证器。使用 StoreUserRequest 作为参数 en 然后您可以使用 $request 变量访问经过验证的数据。
    • public function doLogin(StoreUserRequest $request) 是我如何使用它作为参数的吗?既然您说我应该删除验证器并改用 $request,您的意思是我将我的 if 条件更改为 if($request->fails())?另外,我得到一个错误 Class StoreUserRequest 不存在
    【解决方案2】:

    在命名空间之后的顶部使用验证器

    use Validator;
    

    这真的是因为我必须制作验证器类吗?

    不,Validator 是外观,您需要在顶部使用它,否则它会在您的控制器类中找到。这就是为什么你得到'App\Http\Controllers\Validator' not found

    【讨论】:

    • 所以,我试了一下,因为 Input:all() 报错,我该怎么办?
    • 你可以尝试使用 $request->all() 代替。
    • 所以,我刚刚意识到我还没有阅读过有关请求的内容,这就是我无法理解这一点的原因。所以,如果我做对了,我应该将 $ request 参数添加到我的 doLogin 函数中,然后将我的 Input::all() 更改为 $request->all(),对吗?另外,由于我的post方法路径是/home,它会自动获取输入的数据吗?而且,正如您在我的 doLogin 函数中看到的那样,我还获取输入的数据并将其放入变量 $userdata 的数组中,所以由于我将使用 $request,这是否意味着我可以使用 $request->input('login -电子邮件')?
    • $request-&gt;login-email你可以试试这个方法
    • 我尝试这样做,但我无法使用 $request->login-email。我显示错误,所以我使用我之前说的那个。但是现在,我不明白为什么我的验证器总是失败,即使我的输入已经满足了要求。另外,如果我使用 redirect::to 总是会出错,我必须添加使用或命名空间吗?
    猜你喜欢
    • 2016-09-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2013-05-07
    • 2015-05-05
    相关资源
    最近更新 更多