bllim/laravalid
Composer 安装命令:
composer require bllim/laravalid
包简介
This package makes validation rules defined in laravel work client-side by converting to html/js plugins such as jquery validation. It also allows to use laravel validation messages so you can show same messages for both sides.
关键字:
README 文档
README
Laravel Validation For Client Side
This package makes validation rules defined in laravel work client-side by converting to html/js plugins such as jquery validation. It also allows to use laravel validation messages so you can show same messages for both sides.
Table of contents
- Feature Overview
- Installation
- Configuration
- Usage
- Extending
- Plugins and Supported Rules
- Known Issues
- To Do
- Contributors
- Licence
Feature Overview
- Multi-Plugin Support //For now, there is just one :)
Jquery Validation
- Extendible
- Laravel form builder based
- Validation rules can be set from controller
- Distinguishing between numeric input and string input
- User friendly input names
- Remote rules such as unique and exists
Installation
Require bllim/laravalid in composer.json and run composer update.
{
"require": {
"laravel/framework": "5.2.*", //or "5.0.*"
...
"bllim/laravalid": "*"
}
...
}
Note: For Laravel 4 use
laravel4branch like as"bllim/laravalid": "dev-laravel4"or"~0.9"
Composer will download the package. After the package is downloaded, open config/app.php and add the service provider and alias as below:
'providers' => array( ... Bllim\Laravalid\LaravalidServiceProvider::class, ),
'aliases' => array( ... 'HTML' => Collective\Html\HtmlFacade::class, // if not exists add for html too 'Form' => Bllim\Laravalid\Facade::class, ),
Also you need to publish configuration file and assets by running the following Artisan commands.
$ php artisan vendor:publish
Configuration
After publishing configuration file, you can find it in config folder as laravalid.php file. Configuration parameters are as below:
| Parameter | Description | Values |
|---|---|---|
| plugin | Choose plugin you want to use | See Plugins and Supported Rules |
| useLaravelMessages | If it is true, laravel validation messages are used in client side otherwise messages of chosen plugin are used | true/false |
| route | Route name for remote validation | Any route name (default: laravalid) |
Usage
The package uses laravel Form Builder to make validation rules work for both sides. Therefore you should use Form Builder. While opening form by using Form::open you can give $rules as second parameter:
$rules = ['name' => 'required|max:100', 'email' => 'required|email', 'birthdate' => 'date']; Form::open(array('url' => 'foo/bar', 'method' => 'put'), $rules); Form::text('name'); Form::text('email'); Form::text('birthdate'); Form::close(); // don't forget to close form, it reset validation rules
Also if you don't want to struggle with $rules at view files, you can set it in Controller or route with or without form name by using Form::setValidation($rules, $formName). If you don't give form name, this sets rules for first Form::open
// in controller or route $rules = ['name' => 'required|max:100', 'email' => 'required|email', 'birthdate' => 'date']; Form::setValidation($rules, 'firstForm'); // you can also use without giving form name Form::setValidation($rules) because there is just one. // in view Form::open(array('url' => 'foo/bar', 'method' => 'put', 'name' => 'firstForm'), $rules); // some form inputs Form::close();
For rules which is related to input type in laravel (such as max, min), the package looks for other given rules to understand which type is input. If you give integer or numeric as rule with max, min rules, the package assume input is numeric and convert to data-rule-max instead of data-rule-maxlength.
$rules = ['age' => 'numeric|max'];
The converter assume input is string by default. File type is also supported.
Validation Messages
Converter uses validation messages of laravel (resources/lang/en/validation.php) by default for client-side too. If you want to use jquery validation messages, you can set useLaravelMessages, false in config file of package which you copied to your config dir.
Plugins
Jquery Validation
While using Jquery Validation as html/js validation plugin, you should include jquery.validate.laravalid.js in your views, too. After assets published, it will be copied to your public folder. The last thing you should do at client side is initializing jquery validation plugin as below:
<script type="text/javascript"> $('form').validate({onkeyup: false}); //while using remote validation, remember to set onkeyup false </script>
Example
Controller/Route side
class UserController extends Controller { static $createValidations = ['name' => 'required|max:255', 'username' => 'required|regex:/^[a-z\-]*$/|max:20', 'email' => 'required|email', 'age' => 'numeric']; public function getCreate() { Form::setValidation(static::$createValidations); return View::make('user.create'); } public function postCreate() { $inputs = Input::only(array_keys(static::$createValidations)); $validator = Validator::make($inputs, static::$createValidations); if ($validator->fails()) { // actually withErrors is not really necessary because we already show errors at client side for normal users return Redirect::back()->withErrors($validator); } // try to create user return Redirect::back()->with('success', 'User is created successfully'); } }
View side
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Laravalid</title> </head> <body> {{ Form::open('url'=>'create', 'method'=>'post') }} {{ Form::text('name') }} {{ Form::text('username') }} {{ Form::email('email') }} {{ Form::number('age') }} {{ Form::close() }} <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script> <script src="{{ asset('vendor/laravalid/jquery.validate.laravalid.js') }}"></script> <script type="text/javascript"> $('form').validate({onkeyup: false}); </script> </body> </html>
Extending
There are two ways to extend package with your own rules. First, you can extend current converter plugin dynamically like below:
Form::converter()->rule()->extend('someotherrule', function($parsedRule, $attribute, $type){ // some code return ['data-rule-someotherrule' => 'blablabla']; }); Form::converter()->message()->extend('someotherrule', function($parsedRule, $attribute, $type){ // some code return ['data-message-someotherrule' => 'Some other message']; }); Form::converter()->route()->extend('someotherrule', function($name, $parameters){ // some code return ['valid' => false, 'messages' => 'Seriously dude, what kind of input is this?']; });
Second, you can create your own converter (which extends Base\Converter or any current plugin converter) in Bllim\Laravalid\Converter\ namespace and change plugin configuration in config file with your own plugin name.
Note: If you are creating a converter for some existed html/js plugin please create it in
Converterfolder and send a pull-request.
Plugins and Supported Rules
Jquery Validation
To use Jquery Validation, change plugin to JqueryValidation in config file and import jquery, jquery-validation and jquery.validate.laravalid.js in views.
| Rules | Jquery Validation |
|---|---|
| Accepted | - |
| Active URL | + |
| After (Date) | + |
| Alpha | + |
| Alpha Dash | - |
| Alpha Numeric | + |
| Array | - |
| Before (Date) | + |
| Between | + |
| Boolean | - |
| Confirmed | - |
| Date | + |
| Date Format | - |
| Different | + |
| Digits | - |
| Digits Between | - |
+ |
|
| Exists (Database) | + |
| Image (File) | + |
| In | - |
| Integer | + |
| IP Address | + |
| Max | + |
| MIME Types | + |
| Min | + |
| Not In | - |
| Numeric | + |
| Regular Expression | + |
| Required | + |
| Required If | - |
| Required With | + |
| Required With All | - |
| Required Without | + |
| Required Without All | - |
| Same | + |
| Size | - |
| String | - |
| Timezone | - |
| Unique (Database) | + |
| URL | + |
Note: It is easy to add some rules. Please check
Ruleclass of related converter.
Contribution
You can fork and contribute to development of the package. All pull requests is welcome.
Conversion Logic
Package converts rules by using converters (in src/Bllim/Laravalid/Converter). It uses Converter class of chosen plugin which extends Converter/Base/Converter class.
You can look at existed methods and plugins to understand how it works. Explanation will be ready, soon.
Known issues
- Some rules are not supported for now
TODO
- Support unsupported rules
- Improve doc
- Comment code
Contributors
- @bllim
- @phpspider
- @jannispl
- @rene-springmann
- @nthachus
and more
License
Licensed under the MIT License
bllim/laravalid 适用场景与选型建议
bllim/laravalid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.29k 次下载、GitHub Stars 达 58, 最近一次更新时间为 2015 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「laravel」 「client-side」 「jquery validation」 「laravel5」 「laravel validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bllim/laravalid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bllim/laravalid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bllim/laravalid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Convert Symfony constraints into data-attributes for client-side validation with Parsley.
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A simple, extensible validation library for PHP with support for filtering and validating any input array along with generating client side validation code.
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
统计信息
- 总下载量: 15.29k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 59
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-16