fadion/rule 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fadion/rule

Composer 安装命令:

composer require fadion/rule

包简介

An expressive validation rule builder for Laravel.

README 文档

README

A simple Laravel package that provides an expressive alternative to building validation rules. It uses what you already know about validation with Laravel and builds on top of that.

Instead of this:

$rules = [
    'username' => 'required|alpha',
    'email' => 'required|email'
];

You'll be writing:

Rule::add('username')->required()->alpha();
Rule::add('email')->required()->email();

$rules = Rule::get();

Which method is more easy to read or write is a matter of personal preference, so I'm not taking sides. However, using Rule offers two main advantages:

  1. Misstyped rule names, non existing rules or missing arguments will throw fatal errors. In contrast, Laravel's rules will fail silently and provide no information when you write 'reqiured' instead of 'required'.
  2. IDE suggestions and auto complete. Every rule is a real, documented method that your IDE can easily pick up. Coupled with Laravel IDE Helper, you'll rarely need to open the Validator docs.

Installation

  1. Add the package to your composer.json file and run composer update:
{
    "require": {
        "fadion/rule": "~1.1"
    }
}
  1. Add Fadion\Rule\RuleServiceProvider::class to your config/app.php file, inside the providers array.

  2. Add a new alias: 'Rule' => Fadion\Rule\Facades\Rule::class to your config/app.php file, inside the aliases array.

For Laravel 4, use version 1.0

Usage

If you've used Laravel's Validator, then you already know how to use Rule. It just changes the way you write rules, but the validation process remains exactly the same.

A complete validation example

$inputs = Input::all();

Rule::add('username')->required()->alpha();
Rule::add('email')->required()->email();

$validator = Validator::make($inputs, Rule::get());

if ($validator->fails()) {
    Return::back()->withInput()->withErrors($validator);
}

Rules with parameters

Rule parameters are handled as simple method arguments. There can be one or more arguments, depending on the rule and some can be arrays too (like: in, not_in, mimes, etc.).

Rule::add('date')->date_format('mm/dd/YYYY');
Rule::add('age')->between(5, 15);
Rule::add('role')->in(['Admin', 'Moderator', 'Editor']);

Exists and Unique

exists and unique are special cases, as they have a few defined and documented arguments, as well as dynamic ones for the where clauses.

Rule::add('username')->exists('users');
Rule::add('username')->exists('users', 'name');
Rule::add('username')->exists('users', 'name', 'id', 10);


Rule::add('email')->unique('users');
Rule::add('email')->unique('users', 'email_address', 10);
Rule::add('email')->unique('users', 'email_address', 10, 'account_id', 1);

Custom rules

Rule handles custom rules in the same way as the predefined ones. It even accepts parameters.

Validator::extend('foo', function($attribute, $value, $parameters) {
    return $value == 'foo';
});

Rule::add('name')->required()->foo();

Array rule

Laravel's rule for validating an input as array is renamed to is_array(). The word "array" is reserved in PHP and can't be used as a method name, hence the rename.

Rule::add('languages')->is_array();

Method names

Methods can be written as either snake_case (as Laravel accepts rule names) or as camelCase. Both cases behave exactly the same, so use whatever feels better.

So, the methods below are equivalent:

Rule::add('date')->date_format('Y-m-d');
Rule::add('date')->dateFormat('Y-m-d');

Rule::add('username')->alpha_dash();
Rule::add('username')->alphaDash();

Attribute names

Laravel has an option to alias input names with custom attributes, as a way to build better error messages. Rule provides an easy way to create them.

Rule::add('name', 'Your name')->required();
Rule::add('email', 'Your email')->required()->email();

$validator = Validator::make(Input::all(), Rule::get());

// Apply attributes
$validator->setAttributeNames(Rule::getAttributes());

Messages

There's no way that you start writing expressive rules, but keep messages as arrays. Rule will handle those too in a very elegant and easy to use way. Just add a message() method after the rule:

Rule::add('email')
    ->required()->message("Email shouldn't be empty.")
    ->email()->message("Email appears to be invalid.");

Rule::add('password')
    ->required()->message("Password shouldn't be empty.")
    ->between(5, 15)->message("Make that password more secure!");

$rules = Rule::get();
$messages = Rule::getMessages();

统计信息

  • 总下载量: 1.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-05-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固