本书未发布

表单验证

未匹配的标注

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

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
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 0
发起讨论 只看当前版本


暂无话题~