开发有个原则是永远不能信任用户输入的数据;
即便前端已经做了验证;
在后端 php 也必须要再次验证;
laravel 为表单验证提供了强大且简单的方案;
创建示例路由:
routes/web.php
[cc]Route::prefix(‘validation’)->group(function () {
Route::get(‘create’, ‘ValidationController@create’);
Route::post(‘store’, ‘ValidationController@store’);
Route::get(‘edit’, ‘ValidationController@edit’);
Route::post(‘update’, ‘ValidationController@update’);
});[/cc]

创建控制器
[cc]php artisan make:controller ValidationController –resource[/cc]
app/Http/Controllers/ValidationController.php
[cc]public function create()
{
return view(‘validation.create’);
}[/cc]

创建视图;
这里直接把官方自带的注册页面复制过来做示例了;
为了方便验证后台我把 html 标签的验证删除了;
并增加了一个 tag 选项;
resources/views/validation/create.blade.php

[cc]@extends(‘layouts.app’)

@section(‘content’)

注册
{{ csrf_field() }}

@if ($errors->has(‘tag’))

{{ $errors->first(‘tag’) }}

@endif

@if ($errors->has(‘name’))

{{ $errors->first(‘name’) }}

@endif

@if ($errors->has(’email’))

{{ $errors->first(’email’) }}

@endif

@if ($errors->has(‘password’))

{{ $errors->first(‘password’) }}

@endif

@endsection[/cc]

发表回复

后才能评论