本书未发布

表单验证

未匹配的标注

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

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
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
发起讨论 查看所有版本


暂无话题~