总结7.20 laravel自动验证
方式一:控制器内直接验证
Input::method()得到获取方法;
public function valite(Request $request){
$this->validate($request,[
'title' => "required" //验证的规则
],[
'title.required' => "标题是必填的"
]);
}
方式二,独立方式去验证表单
namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller
//需要引入验证和控制器的各种类;
class PostController extends Controller
{
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
}
}
方式三、验证器
php artisan make:request 验证器的名称
public function aaaa(){
return true;
}
public function rule()
{
return [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
}