本书未发布

表单验证

未匹配的标注

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

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
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~