表单验证的$errors无法在session中获取
1新建表单验证规则
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class LoginValidate 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 [
'firstName' => 'required|string|max:255',
'lastName' => 'required|string|max:255',
'email' => 'required|email|unique:user,email',
'password' => 'required|same:passwordconf',
'captcha' => 'required|checkcaptcha',
];
}
public function message()
{
return [
'captcha.required'=>'验证码不能为空',
'captcha.checkcaptcha'=>'输入验证码错误',
'email.required'=>'邮箱不能为空',
'email.email'=>'邮箱格式错误',
'email.unique'=>'邮箱已注册',
'password.required'=>'密码不能为空',
'password.min'=>'密码长度在6-20',
'password.same'=>'两次密码输入不一致',
];
}
}
2控制器中调用该规则
<?php
/**
Created by PhpStorm.
User: Administrator
Date: 2018-06-01
Time: 15:44
*/
namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\LoginValidate;
use Carbon\Carbon;
use DB;
use Validator;
use Illuminate\Support\Facades\Session;
class RegisterController extends Controller
{
public function index()
{
return view('home.register');
}
public function doregister(LoginValidate $request)
{
$id = DB::table('user')->insertGetId(
[
'user_name' => $request->firstName.' '.$request->firstName,
'firstName' => $request->firstName,
'lastName' => $request->lastName,
'email' => $request->email,
'password' => $request->password,
'reg_time' => Carbon::now()
]
);
//
return redirect('home/login');
}
}
3blade文件接收并显示验证返回的错误
<div class="login-form-grids">
@if (count($errors)>0)
<div class="alert alert-info">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<!-- <li>{{count($errors)}}****{{var_dump(Session::all())}}</li> -->
<form action="/home/doregister" method="post">{{csrf_field()}}
<input type="text" placeholder="First Name..." required=" " value="{{old('firstName')}}" name="firstName">
<input type="text" placeholder="Last Name..." required=" " value="" name="lastName">
<input type="email" placeholder="Email Address" required=" " value="" name="email">
<input type="password" placeholder="Password" required=" " value="" name="password">
<input type="password" placeholder="Password Confirmation" required=" " name="passwordconf" style="margin-bottom:14px">
<div class="row" margin="1em">
<div class="col-md-8">
<input type="text" class="form-control {{$errors->has('captcha')?'parsley-error':''}}" name="captcha"
placeholder="captcha" required=" " style="height:42px">
</div>
<div class="col-md-4">
<img src="{{url('/loadcaptcha/1')}}" style="cursor: pointer" onclick="this.src='{{url('/loadcaptcha')}}/'+Math.random()">
</div>
</div>
<input type="submit" value="Register">
</form>
</div>
使用这种方法的表单验证在laravel5.6永远无法获取到$errors的数据,一直是0,求大神帮忙看看该怎么解决
另外目前用5.6版本能否换成5.5版本,如果可以的话该怎么换呢?
No Comments