flatphp / validation
light validator
v1.0.0
2020-03-08 09:12 UTC
Requires
- php: >=5.5
This package is auto-updated.
Last update: 2024-12-08 21:52:00 UTC
README
light validator
Install
composer require flatphp/validation
Usage
use \Flatphp\Validation\Validator; // data value $data = ['username' => 'tom', 'email' => '', 'password' => '', 'age' => 10]; // validate rules (验证规则和错误消息) $rules = array( 'username' => ['required|length:3,20', array('required' => 'username required', 'length' => 'length range 3-20')], 'email' => ['email', 'invalid email'], 'password' => 'required' ); // validate $res = Validator::validate($data, $rules, $failed); // if failed echo failed message if (!$res) { echo $failed['msg']; } // failed is referenced value if fail, format: ['on' => 'required', 'msg' => 'username required']
failed referenced value $failed 格式
// failed e.g.
array(
'on' => 'required',
'msg' => 'username required'
)
single value validate
use \Flatphp\Validation\Validator; $value = 'hello'; $res = Validator::validateOne($value, 'required|length:2,20', array( 'required' => 'cannot be empty', ), $failed);
simple use
$res = Validator::isEmail('test@gmail.com');
How to custom your own validate method 加入定制自己的验证方法
- Just extends Validator class and add your own method (继承)
- Or Just write global function (写全局函数)
notes:
method|function should be like isXxx($value) or isXxx($value, $params), example:
方法或函数格式必须是这样的,参考以下:
function isZero($value) { return $value === 0; } function isHasstr($value, $param) { return strpos($value, $param) !== false; }