500 内部服务器错误 - 表单验证 (Laravel)



所以,基本上我尝试在Laravel中创建表单验证,在这种情况下是注册表单验证。我尝试使用请求类创建此验证。问题是,验证不适用于电子邮件和用户名字段,这是唯一的(两个字段(和电子邮件格式验证。相反,我得到了这个错误。其他字段验证工作正常。

在我决定尝试使用请求类之前,我尝试在控制器内部进行验证,它工作正常。我不知道为什么这种方法不起作用。

注册刀片.php

<link rel="stylesheet" href="/css/sweetalert2.min.css">
<link rel="stylesheet" href="/css/app.css">
<link rel="stylesheet" href="/css/register-style.css">
<meta name="csrf-token" content="{{ csrf_token() }}">
<script src="/js/app.js"></script>
<title>Register</title>
<body>
<div class="jumbotron vertical-center">          
<div class="container">             
<h1>Register</h1>
<form>
<div class="alert alert-danger">
</div> 
<div id="form-input">    
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" name="email">
</div>
<div class="form-group">
<label for="exampleInputUsername1">Username</label>
<input type="text" class="form-control" id="exampleInputUsername1"name="username">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password">
</div>
<div class="form-group">
<label for="exampleInputConfPassword1">Confirm Password</label>
<input type="password" class="form-control" id="exampleInputConfPassword1" name="password_confirmation">
</div>
</div>
<button id="btnRegister" type="submit" class="btn btn-primary" name="btnRegister">Register</button>
</form> 
<a href="/">Back to login menu</a>
</div>
</div>
</body>
<script src="/js/sweetalert2.all.min.js"></script>
<script src="/js/register-script.js"></script>

注册控制器.php

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesInput;
use IlluminateSupportFacadesHash;
use IlluminateSupportFacadesDB;
use AppHttpRequestsRegisterRequest;
class RegisterController extends Controller
{
public function Index(){
return view('register');
}
public function insertNewUser(RegisterRequest $req){        
DB::table('users')->insert([
'email' => $req['email'],
'username' => $req['username'],
'password' =>  Hash::make($req['password'])
]);
return response()->json(['response' => 'success']);
}
}

注册请求.php

<?php
namespace AppHttpRequests;
use IlluminateFoundationHttpFormRequest;
use IlluminateValidationRule;
class RegisterRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|email|unique:users,email',
'username' => 'required|unique:users,username',
'password' => 'required|min:6|confirmed'
];
}
}

除了浏览器控制台上显示的 500 错误外,我还在 Laravel 日志中收到了错误消息:

[2019-08-07 13:27:04] local.ERROR: Method IlluminateHttpRequest::validated does not exist. {"exception":"[object] (BadMethodCallException(code: 0): Method Illuminate\Http\Request::validated does not exist.

任何帮助将不胜感激。谢谢!

您已经在使用请求类,那么它会自动将规则应用于您的 post 请求(依赖注入(,您无需在 ValidateRegister 中再次调用它

public function ValidateRegister(RegisterRequest $req){
return $this->insertNewUser(
$req['email'],
$req['username'],
Hash::make($req['password'])
);
}

最终,该功能完全没有必要,并且浪费了线路

您可以在插入新用户函数中组合它

public function insertNewUser(RegisterRequest $req){        
DB::table('users')->insert([
'email' => $req['email'],
'username' => $req['username'],
'password' => Hash::make($req['password'])
]);
return response()->json(['response' => 'success']);
}

由于您使用的是表单请求,因此不需要在控制器中$validated = $req->validated();。注入自定义请求类时,验证将在请求数据到达控制器之前自行运行。

为了使您的请求类更干净一些,您不需要那里confirmPassword字段。只需使用confirmed规则进行password并将confirmPassword字段重命名为表单中的password_confirmation,它就可以工作。

相关内容

  • 没有找到相关文章

最新更新