laravel5.5-第4章-表单验证 laravel5.5-第4章-表单验证

2023-07-03

一、下载 laravel 5.5

composer create-project laravel/laravel=5.5.* laravel5.5_form

新建数据库 laravel5.5_form

修改 .evn 配置文件

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel5.5_form
DB_USERNAME=laravel5.5_form
DB_PASSWORD=laravel5.5_form

修改中国时区,在 config/app.php 中修改

'timezone' => 'PRC',

切换目录

cd laravel5.5_form

二、创建模型

创建 post model

php artisan make:model Post -m

修改迁移文件:posts_table.php

public function up()
{
   Schema::create('posts', function (Blueprint $table) {
       $table->increments('id');
       $table->string('title');
       $table->text('content');
       $table->timestamps();
   });
}

执行数据迁移

php artisan migrate

如果报错

Specified key was too long; max key length is 1000 bytes

问题解决:在 AppServiceProvider 中调用 Schema::defaultStringLength 方法来实现配置:

use Illuminate\Support\Facades\Schema;

public function boot()
{
   Schema::defaultStringLength(191);
}

三、新版错误提示

创建 PostController

php artisan make:controller PostController

修改 app/Http/Controllers/PostController.php

public function create()
{
   return view('posts.form');
}

public function store(Request $request)
{
   return $request->all();
}

新建视图文件:resources/views/posts/form.blade.php

https://file.lulublog.cn/images/3/2023/07/LPYFSq8EjPeEQ8yQiqeE9jjjkQ88ee.jpg

这里引入了 CSS Bulma 框架:https://bulma.io/documentation/overview/start/

注册路由,在 routes/web.php 新增

Route::resource('posts', 'PostController');

启动 serve

php artisan serve

访问:http://127.0.0.1:8000/posts/create

https://file.lulublog.cn/images/3/2023/07/I4fqDjt1ZFgWo141bgqw0OBHGepeZw.jpg

将视图文件:resources/views/posts/form.blade.php 中的

{{ csrf_field() }}

删除,然后点击提交按钮

https://file.lulublog.cn/images/3/2023/07/Vrw9aZ0ZCbwW6wQ9Q66vqrqDz6o0qk.jpg

这是 laravel 5.5 的错误提示

laravel 5.4 的错误提示

https://file.lulublog.cn/images/3/2023/07/Riaz4FBfvAV2M94f9kl9lKvzK8BpaI.jpg

相对于之前的提示,laravel 5.5 的提示更友好

四、旧版表单验证

laravel 5.5 之前的表单验证方式

修改 app/Post.php

protected $fillable = [
   'title', 'content'
];

修改 app/Http/Controllers/PostController.php

use App\Post;

public function store(Request $request)
{
   $this->validate(request(), [
       'title' => 'required',
       'content' => 'required',
   ]);

   Post::create(request(['title', 'content']));

   return 'success';
}

访问:http://127.0.0.1:8000/posts/create,Title 和 Content 随便输入后点击提交按钮

https://file.lulublog.cn/images/3/2023/07/yODZCODdB2WWfW5Z14oG46GDW2wWEa.jpg

五、新版表单验证

laravel 5.5 的表单验证方式

修改 app/Http/Controllers/PostController.php

use App\Post;

public function store(Request $request)
{
   $post = $request->validate([
       'title' => 'required',
       'content' => 'required',
   ]);

   Post::create($post);

   return 'success';
}

访问:http://127.0.0.1:8000/posts/create,Title 和 Content 随便输入后点击提交按钮

https://file.lulublog.cn/images/3/2023/07/yODZCODdB2WWfW5Z14oG46GDW2wWEa.jpg

六、自定义表单验证规则

创建验证器

php artisan make:rule TitleValidate

修改生成的文件:app/Rules/TitleValidate.php

public function passes($attribute, $value)
{
   return preg_match('/^\S[\u4e00-\u9fa5]+\S$/', $value);
}

public function message()
{
   return '标题不允许含特殊字符';
}

修改 app/Http/Controllers/PostController.php

public function store(Request $request)
{
   $post = $request->validate([
       'title' => ['required', new TitleValidate()],
       'content' => 'required',
   ]);

   Post::create($post);

   return 'success';
}

新建错误代码文件:resources/views/errors/list.blade.php

https://file.lulublog.cn/images/3/2023/06/Tuu6x5Gzuf5XyFs5P2LU5xPXZcPpZu.jpg

修改:resources/views/posts/form.blade.php,将错误代码放在 /form 下面一行

@include('errors/list')

访问:http://127.0.0.1:8000/posts/create

Title 输入包含特殊字符,Content 随便输入,点击提交按钮

https://file.lulublog.cn/images/3/2023/07/O74Du4DqJqVEXvD6jj3esqNQGxqDh6.jpg

阅读 418