Home php教程 php手册 Laravel 5框架学习之表单验证

Laravel 5框架学习之表单验证

Jun 06, 2016 pm 08:05 PM

Laravel 通过 Validation 类让您可以简单、方便的验证数据正确性及查看相应的验证错误信息。如果是更复杂的验证场景,你可能需要创建一个表单请求。表单请求是一

在建立一个文章的时候,如果你什么都不输入直接提交,ok,你获得了一个空的文章,没有任何错误提示,这是不对的。在命令行下运行 php artisan 可以看到一个选项 make:request,新建一个form request类。在命令行执行

复制代码 代码如下:


php artisan make:request CreateArticleRequest

生成的文件在 app/http/requests 目录下。在文件中我们可以看到两个方法:

public function authorize() { return false; } public function rules() { return [ // ]; }

authorize 表示用户在提交表单的时候是否需要是认证用户,我们不需要认证,返回 true。rules是我们的规则方法。让我们修改这个方法:

public function authorize() { //修改为 true,表示不需要认证,或者是通过认证 return true; } public function rules() { return [ 'title' => 'required|min:3', 'body' => 'required', 'published_at' => 'required|date' ]; }

其他的约束可以插看 laravel 的文档。上面的约束表示 title 是必须输入的,最少3个字符,body 是必须的,published_at 是必须的而且是日期。

在视图中,我们总是可以访问 $errors 变量来判断我们是否有错误,修改视图

@if ($errors->any())

    @foreach($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::open(['url' => 'articles']) !!}

修改控制器,引入我们的 Request 类。

public function store(Requests\CreateArticleRequest $request) { Article::create($request->all()); return redirect('articles'); }

再次提交表单,什么都不填,可以看到错误信息。

修改提示信息变为中文

显示的是英文的错误信息,,实际上laravel考虑到了国际化的问题,首先修改 config/app.php ,

复制代码 代码如下:


 'locale' => 'zh',

将 locale 语言设置为中文,然后再 resources/lang 下面新建文件夹 zh, 拷贝 resources/lang/en/validation.php 文件到 zh 目录下,修改:

"min" => [ "numeric" => "The :attribute must be at least :min.", "file" => "The :attribute must be at least :min kilobytes.", "string" => ":attribute 至少要包含 :min 字符。", "array" => "The :attribute must have at least :min items.", ], "required" => ":attribute 必须填写。",

其他的可以自行翻译。再次提交空表单,错误信息为中文了。而且 min:3 的判断也为最少3个中文。

--

laravel 也在控制器中集成了 validate 方法,换句话说,我们不一定要生成 request 类,这些工作我们可以直接在控制器中完成。

修改控制器:

//注意 Request 的命名空间,不要弄错了 public function store(\Illuminate\Http\Request $request) { $this->validate($request, [ 'title' => 'required|min:3', 'body' => 'required', 'published_at' => 'required|date' ]); Article::create($request->all()); return redirect('articles'); }

结果相同,这样可以更快速的完成简单的验证。

以上所述就是本文给大家分享的全部内容了,希望能够对大家熟练掌握Laravel5框架有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)