使用验证器验证Laravel中的用户输入

bkhjykvo  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(156)

我尝试在Laravel中使用RegisterController,但是我无法让Validator工作。我不明白问题出在哪里,因为它应该只接受一个数组并对其进行验证。
当我尝试将一个具有正确字段的JSON发送到注册路由时,我收到了以下错误:

BadMethodCallException: Method validate does not exist. in file /home/deb85528n3/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php on line 96

下面是我的代码:

protected function validator(array $data)
{       
    $validator = Validator::make($data,
        [
        'first_name' => 'required|string|max:255',
        'last_name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
        'birth_year' => 'required|integer',
        'lat' => 'required',
        'lon' => 'required',
        ]);

    echo $validator->errors();

   if ($validator->fails())
   {
        return response()->json(['errors'=>$validator->errors()]);
   }

   if ($validator->passes())
   {
        $response = "validator passed";
        return response()->json($response);
   }

}

我还尝试以不同的方式使用Validator:

public function validator(Request $request){
    $validator = Validator::make($request->all(), [
        'first_name' => 'required|string|max:255',
        'last_name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
        'birth_year' => 'required|integer|min:4',
        'lat' => 'required|numeric',
        'lon' => 'required|numeric',
    ]);
}

但是我得到了这个错误:

Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to App\Http\Controllers\Auth\RegisterController::validator() must be an instance of App\Http\Controllers\Auth\Request, array given, called in /home/deb85528n3/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php on line 31 in file /home/deb85528n3/app/Http/Controllers/Auth/RegisterController.php on line 103

编辑以包括整个RegisterController:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = '/home';

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

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    $validator = Validator::make($data, [
        'first_name' => 'required|string|max:255',
        'last_name'  => 'required|string|max:255',
        'email'      => 'required|string|email|max:255|unique:users',
        'password'   => 'required|string|min:6|confirmed',
        'birth_year' => 'required|integer|min:4',
        'lat'        => 'required|numeric',
        'lon'        => 'required|numeric',
    ]);

    echo $validator->errors();

   if ($validator->fails())
   {
        return response()->json(['errors'=>$validator->errors()]);
    }

    if ($validator->passes())
    {
        $response = "validator passed";
        return response()->json($response);
    }
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
protected function create(array $data)
{
    //maybe check if facebook login here?
    return User::create([
        'first_name' => $data['first_name'],
        'last_name' => $data['last_name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'birth_year' => $data['birth_year'],
        'lat' => $data['lat'],
        'lon' => $data['lon'],
    ]);
}

}

bfrts1fy

bfrts1fy1#

更改函数名

use Validator;

把这个加到你的控制器里,
删除此

public function validator(Request $request){

在模型中编写如下验证规则

public static function rules()
    {
       return [
         'first_name' => 'required|string|max:255',
         'last_name' => 'required|string|max:255',
         'email' => 'required|string|email|max:255|unique:users',
         'password' => 'required|string|min:6|confirmed',
         'birth_year' => 'required|integer|min:4',
         'lat' => 'required|numeric',
         'lon' => 'required|numeric',
       ];
     }

并在控制器中调用此规则

$validator = Validator::make($request->all(), "Your model Name"::rules()); 
  if ($validator->fails()) {
        //throw exception
    }
cbeh67ev

cbeh67ev2#

register方法会呼叫validator方法并传递数组,然后预期会传回\Illuminate\Contracts\Validation\Validator的执行严修。
除非您需要覆盖注册用户的默认响应,否则您应该只需要:

public function validator(array $data)
{
    return Validator::make($data, [
        'first_name' => 'required|string|max:255',
        'last_name'  => 'required|string|max:255',
        'email'      => 'required|string|email|max:255|unique:users',
        'password'   => 'required|string|min:6|confirmed',
        'birth_year' => 'required|integer|min:4',
        'lat'        => 'required|numeric',
        'lon'        => 'required|numeric',
    ]);
}

相关问题