amitkhare / easy-validation
Easy Validation 是一个简单易用的 PHP 验证库
0.5.9
2017-12-14 08:08 UTC
Requires
- php: >=5.5.0
- amitkhare/easy-translator: ^0
README
Easy Validation 是一个易于使用的 PHP 验证库
安装
从您想要安装的目录运行此命令。
通过 Composer
php composer.phar require amitkhare/easy-validation
通过 Git
git clone https://github.com/amitkhare/easy-validation.git
手动安装
Download: https://github.com/amitkhare/easy-validation/archive/master.zip
Extract it, require "PATH-TO/"."EasyValidation.php" where you want to use it.
最小使用示例
<?php require("PATH-TO/"."EasyValidation.php"); // only need to include if installed manually. $v = new AmitKhare\EasyValidation\EasyValidation(); // instantiate EasyValidation; $v->setSource($_POST); // set data source array; $v->setLocale("en-IN","PATH/TO/LOCALES/DIRECTORY/"); $v->check("email","required|email|unique:users.email|min:4|max:100"); $v->match("password","password_confirm","required|min:6|max:25"); if(!$v->isValid()){ print_r($v->getStatus()); }
用法
<?php use AmitKhare\EasyValidation; // use namespace. require("PATH-TO/"."EasyValidation.php"); // only need to include if installed manually. $v = new EasyValidation(); // instantiate EasyValidation; // OR with database for unique field check $v = new EasyValidation($host,$username,$password,$dbname); // instantiate EasyValidation With Database features; $v->setSource($_POST); // set data source array; // check bottom of this file for sample en-IN.lang file $v->setLocale("en-IN","PATH/TO/LOCALES/DIRECTORY/"); $v->check("mobile","required|numeric|min:10|max:15"); $v->check("username","required|alphanum|unique:users.username|min:4|max:20"); $v->check("email","required|email|unique:users.email|min:4|max:100"); $v->match("password","password_confirm","required|min:6|max:25"); if(!$v->isValid()){ print_r($v->getStatus()); }
可用方法
> $v->setSource(array["FIELD"=>"VALUE"]);
> $v->setLocale("en-IN",PATH); // Path is optional
> $v->check("FIELD","RULES");
> $v->match("FIELD1","FIELD2","RULES");
> $v->isValid(); // returns true/false
> $v->getStatus(); // get error messages
可用规则
> required
> email
> url
> numeric
> string
> float
> ipv4
> ipv6
> bool
> min
> max
> alphanum
> alphanumUnicode
> unique (avaiable only if instantiate EasyValidation With Database Details);
示例 en-IN.lang 文件 [[ JSON 格式 ]]
{ "FIELDS_DONT_MATCH" : "The `%s` dont match with `%s`.", "FIELD_REQUIRED" : "The `%s` is required.", "FIELD_NOT_SET" : "The `%s` field is not set.", "USERNAME":"Username", "FIRSTNAME":"First Name", "LASTNAME":"Last Name", "MIDDLENAME":"Middle Name", "EMAIL":"Email", "PASSWORD":"Password", "MOBILE":"Mobile", "PASSWORD_CONFIRM":"Password Confirm" }
待办事项
> Support for single field check like setField() in addition with setSource()