本书未发布

表单验证

未匹配的标注

文档尚未来得及翻译,欢迎以改进的方式提交译文。

Validation

Introduction

Administrator uses Laravel's validation to validate your models. You can either provide a rules option in your configuration files:

'rules' => array(
    'name' => 'required',
    'age' => 'required|integer|min:18',
)

Or for model pages, you can provide a static $rules property in your Eloquent models like this:

class Movie extends Eloquent {

    /**
     * Validation rules
     */
    public static $rules = array(
        'name' => 'required',
        'age' => 'required|integer|min:18',
    );
}

Now if an admin user tries to save a Movie without an age or an age below 18, Administrator will notify the user of the error and disallow the save from occurring.

Custom Messages

There's a good chance that you'll need to use custom validation messages for each model that you're presenting to your users. In order to do this, you can provide a messages option in your configuration files:

'messages' => array(
    'name.required' => 'The name field is required',
    'age.min' => 'The minimum age is 18 years old',
)

Or for model pages, you can provide a static $messages property in your Eloquent models like this:

class Movie extends Eloquent {

    /**
     * Validation rules
     */
    public static $messages = array(
        'name.required' => 'The name field is required',
        'age.min' => 'The minimum age is 18 years old',
    );
}

Using Aware

If you're already using Aware, then you don't really have to do anything! Aware allows you to define a static $rules property on your Eloquent models, which works exactly like it does in Administrator.

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
Summer
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 0
发起讨论 查看所有版本


暂无话题~