1.webman等php框架的通用validate數(shù)據(jù)驗證器,
2.支持php8。
3.基于thinkphp6修改,支持多sence多場景定義,復(fù)用性高。
4.暫時不提供facede模式,多語言翻譯文件暫時需要自定義在resource/translations/
composer require taoser/webman-validate
定義驗證器
namespace app\validate;
use taoser\Validate;
class User extends Validate
{
protected $rule = [
'name' => 'require|max:25',
'email' => 'email',
];
}
支持創(chuàng)建驗證器進行數(shù)據(jù)驗證
<?php
namespace app\index\validate;
use taoser\Validate;
class User extends Validate
{
protected $rule = [
'name' => 'require|max:25',
'age' => 'number|between:1,120',
'email' => 'email',
];
protected $message = [
'name.require' => '名稱必須',
'name.max' => '名稱最多不能超過25個字符',
'age.number' => '年齡必須是數(shù)字',
'age.between' => '年齡只能在1-120之間',
'email' => '郵箱格式錯誤',
];
}
驗證器調(diào)用代碼如下:
<?php
namespace app\controller;
use app\validate\User;
use taoser\exception\ValidateException;
class Index
{
public function index()
{
try {
validate(User::class)->check([
'name' => 'thinkphp',
'email' => 'thinkphp@qq.com',
]);
} catch (ValidateException $e) {
// 驗證失敗 輸出錯誤信息
dump($e->getError());
}
}
}
多語言提示需要增加以下對應(yīng)翻譯文件:
resource/translations/zh_CN/messages.php
return[
// Validate Error Message
':attribute require' => ':attribute不能為空',
':attribute must' => ':attribute必須',
':attribute must be numeric' => ':attribute必須是數(shù)字',
':attribute must be integer' => ':attribute必須是整數(shù)',
':attribute must be float' => ':attribute必須是浮點數(shù)',
':attribute must be bool' => ':attribute必須是布爾值',
':attribute not a valid email address' => ':attribute格式不符',
':attribute not a valid mobile' => ':attribute格式不符',
':attribute must be a array' => ':attribute必須是數(shù)組',
':attribute must be yes,on or 1' => ':attribute必須是yes、on或者1',
':attribute not a valid datetime' => ':attribute不是一個有效的日期或時間格式',
':attribute not a valid file' => ':attribute不是有效的上傳文件',
':attribute not a valid image' => ':attribute不是有效的圖像文件',
':attribute must be alpha' => ':attribute只能是字母',
':attribute must be alpha-numeric' => ':attribute只能是字母和數(shù)字',
':attribute must be alpha-numeric, dash, underscore' => ':attribute只能是字母、數(shù)字和下劃線_及破折號-',
':attribute not a valid domain or ip' => ':attribute不是有效的域名或者IP',
':attribute must be chinese' => ':attribute只能是漢字',
':attribute must be chinese or alpha' => ':attribute只能是漢字、字母',
':attribute must be chinese,alpha-numeric' => ':attribute只能是漢字、字母和數(shù)字',
':attribute must be chinese,alpha-numeric,underscore, dash' => ':attribute只能是漢字、字母、數(shù)字和下劃線_及破折號-',
':attribute not a valid url' => ':attribute不是有效的URL地址',
':attribute not a valid ip' => ':attribute不是有效的IP地址',
':attribute must be dateFormat of :rule' => ':attribute必須使用日期格式 :rule',
':attribute must be in :rule' => ':attribute必須在 :rule 范圍內(nèi)',
':attribute be notin :rule' => ':attribute不能在 :rule 范圍內(nèi)',
':attribute must between :1 - :2' => ':attribute只能在 :1 - :2 之間',
':attribute not between :1 - :2' => ':attribute不能在 :1 - :2 之間',
'size of :attribute must be :rule' => ':attribute長度不符合要求 :rule',
'max size of :attribute must be :rule' => ':attribute長度不能超過 :rule',
'min size of :attribute must be :rule' => ':attribute長度不能小于 :rule',
':attribute cannot be less than :rule' => ':attribute日期不能小于 :rule',
':attribute cannot exceed :rule' => ':attribute日期不能超過 :rule',
':attribute not within :rule' => '不在有效期內(nèi) :rule',
'access IP is not allowed' => '不允許的IP訪問',
'access IP denied' => '禁止的IP訪問',
':attribute out of accord with :2' => ':attribute和確認字段:2不一致',
':attribute cannot be same with :2' => ':attribute和比較字段:2不能相同',
':attribute must greater than or equal :rule' => ':attribute必須大于等于 :rule',
':attribute must greater than :rule' => ':attribute必須大于 :rule',
':attribute must less than or equal :rule' => ':attribute必須小于等于 :rule',
':attribute must less than :rule' => ':attribute必須小于 :rule',
':attribute must equal :rule' => ':attribute必須等于 :rule',
':attribute has exists' => ':attribute已存在',
':attribute not conform to the rules' => ':attribute不符合指定規(guī)則',
'invalid Request method' => '無效的請求類型',
'invalid token' => '令牌數(shù)據(jù)無效',
'not conform to the rules' => '規(guī)則錯誤',
];
更多用法可以參考6.0完全開發(fā)手冊的驗證章節(jié)
基于thinkphp6 validate修改webman-validate,可用于其它框架。
感謝 ThinkPHP
1.7.1版本,vendor/taoser/webman-validate/src/facade/Validate.php
能不能把facade里面的\think換成你的\taoser,不然使用的時候,ide會警告的
$validate = Validate::rule('code|目標(biāo)code', 'require');
if (!$validate->check($param)) {
throw new ValidateException($validate->getError());
}
check和getError 都會提示
感謝分享