我们可以利用Form Request来封装表单验证代码,从而精简Controller中的代码逻辑,使其专注于业务。而独立出去的表单验证逻辑可以复用到其它请求中,看过几篇文章,大多都是讲怎么创建Request,表面看起来是将逻辑与业务分离了,但是没有做到复用,一个业务就得新建一个Request类实在太累,索性这里我将项目全部的表单验证放在一个Request类里,实现高度可复用,下面是具体实现。
首先创建Request
php artisan make:request CreateUserRequest
CreateUserRequest代码块
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateUserRequest extends Request
{
//验证规则可自己添加需要验证的字段
protected $rules = [
'Student.userName' => 'required|between:2,4',
'Student.userAge' => 'required|integer',
'Student.userSex' => 'required|integer',
'Student.addr' => 'required',
];
//这里我只写了部分字段,可以定义全部字段
protected $strings_key = [
'Student.userName' => '用户名',
'Student.userAge' => '年龄',
'Student.userSex' => '性别',
'Student.addr' => '地址',
];
//这里我只写了部分情况,可以按需定义
protected $strings_val = [
'required'=> '为必填项',
'min'=> '最小为:min',
'max'=> '最大为:max',
'between'=> '长度在:min和:max之间',
'integer'=> '必须为整数',
'sometimes'=> '',
];
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;//修改为true
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = $this->rules;
// 根据不同的情况, 添加不同的验证规则
if (Request::getPathInfo() == '/save')//如果是save方法
{
$rules['Student.addr'] = 'sometimes';
}
if (Request::getPathInfo() == '/edit')//如果是edit方法
{
$rules['Student.addr'] = 'required|min:5';
}
return $rules;
}
//返回给前台的错误信息
public function messages(){
$rules = $this->rules();
$k_array = $this->strings_key;
$v_array = $this->strings_val;
foreach ($rules as $key => $value) {
$new_arr = explode('|', $value);//分割成数组
foreach ($new_arr as $k => $v) {
$head = strstr($v,':',true);//截取:之前的字符串
if ($head) {$v = $head;}
$array[$key.'.'.$v] = $k_array[$key].$v_array[$v];
}
}
return $array;
}
}
控制器具体方法
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function save(\App\Http\Requests\CreateUserRequest $request)
{
//这里会自动调用表单验证
//验证成功后继续向下执行
$data = $request->input('Student');
if(User::create($data)){
return redirect('/')->with('success', '添加成功!');
}else{
return redirect('/create')->with('error', '添加失败!');
}
}
对应的模板文件
<form class="form-horizontal" method="post" action="save">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">姓名</label>
{!! csrf_field() !!}
<div class="col-sm-5">
<input type="text" class="form-control" id="name" name="Student[userName]" placeholder="请输入学生姓名" value="{{ old('Student')['userName']}}">
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">{{ $errors->first('Student.userName') }}</p>
</div>
</div>
<div class="form-group">
<label for="age" class="col-sm-2 control-label">年龄</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="age" name="Student[userAge]" placeholder="请输入学生年龄" value="{{ old('Student')['userAge']}}">
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">{{$errors->first('Student.userAge')}}</p>
</div>
</div>
<div class="form-group">
<label for="age" class="col-sm-2 control-label">地址</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="addr" name="Student[addr]" placeholder="请输地址" >
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">{{$errors->first('Student.addr')}}</p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">性别</label>
<div class="col-sm-5">
<label class="radio-inline">
<input type="radio" name="Student[userSex]" value="1" > 未知
</label>
<label class="radio-inline">
<input type="radio" name="Student[userSex]" value="2"> 男
</label>
![QQ截图20170613152555.png](https://img.haomeiwen.com/i2825702/f008b65789a425f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<label class="radio-inline">
<input type="radio" name="Student[userSex]" value="3"> 女
</label>
</div>
<div class="col-sm-5">
<p class="form-control-static text-danger">{{ $errors->first('Student.userSex') }}</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">提交</button>
</div>
</div>
</form>
网友评论