{"id":20441748,"url":"https://github.com/mix-php/validator","last_synced_at":"2025-10-13T09:49:31.225Z","repository":{"id":57017811,"uuid":"162685329","full_name":"mix-php/validator","owner":"mix-php","description":"Validator based on PSR-7 standard / 基于 PSR-7 标准的验证器","archived":false,"fork":false,"pushed_at":"2021-08-04T07:53:25.000Z","size":51,"stargazers_count":1,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-27T05:11:20.987Z","etag":null,"topics":["mix","validate","validator"],"latest_commit_sha":null,"homepage":"https://openmix.org/mix-php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mix-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-21T08:06:25.000Z","updated_at":"2024-12-16T14:57:37.000Z","dependencies_parsed_at":"2022-08-22T11:31:21.876Z","dependency_job_id":null,"html_url":"https://github.com/mix-php/validator","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/mix-php/validator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fvalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fvalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fvalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fvalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mix-php","download_url":"https://codeload.github.com/mix-php/validator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fvalidator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273892840,"owners_count":25186561,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mix","validate","validator"],"created_at":"2024-11-15T09:34:33.818Z","updated_at":"2025-10-13T09:49:26.172Z","avatar_url":"https://github.com/mix-php.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003e OpenMix 出品：[https://openmix.org](https://openmix.org/mix-php)\n\n# Mix Validator\n\nValidator based on PSR-7 standard\n\n基于 PSR-7 标准的验证器\n\n## Installation\n\n```\ncomposer require mix/validator\n```\n\n## 创建表单\n\n继承 `Mix\\Validator\\Validator` 并定义：\n\n- `public $name` 字段名\n- `rules()` 验证规则\n- `scenarios()` 验证场景\n- `messages()` 错误消息\n\n```php\n\u003c?php\n\nnamespace App\\Forms;\n\nuse Mix\\Validator\\Validator;\n\nclass UserForm extends Validator\n{\n\n    /**\n     * @var string\n     */\n    public $name;\n\n    /**\n     * @var string\n     */\n    public $age;\n\n    /**\n     * @var string\n     */\n    public $email;\n\n    /**\n     * @return array\n     */\n    public function rules(): array\n    {\n        return [\n            'name'  =\u003e ['string', 'maxLength' =\u003e 25, 'filter' =\u003e ['trim']],\n            'age'   =\u003e ['integer', 'unsigned' =\u003e true, 'min' =\u003e 1, 'max' =\u003e 120],\n            'email' =\u003e ['email'],\n        ];\n    }\n\n    /**\n     * @return array\n     */\n    public function scenarios(): array\n    {\n        return [\n            'create' =\u003e ['required' =\u003e ['name'], 'optional' =\u003e ['email', 'age']],\n        ];\n    }\n\n    /**\n     * @return array\n     */\n    public function messages(): array\n    {\n        return [\n            'name.required'  =\u003e '名称不能为空.',\n            'name.maxLength' =\u003e '名称最多不能超过25个字符.',\n            'age.integer'    =\u003e '年龄必须是数字.',\n            'age.unsigned'   =\u003e '年龄不能为负数.',\n            'age.min'        =\u003e '年龄不能小于1.',\n            'age.max'        =\u003e '年龄不能大于120.',\n            'email'          =\u003e '邮箱格式错误.',\n        ];\n    }\n\n}\n```\n\n## 在控制器中验证\n\n```php\n// 使用表单验证\n$form = new UserForm($request-\u003egetAttributes());\nif (!$form-\u003escenario('create')-\u003evalidate()) {\n    $data = ['code' =\u003e 1, 'message' =\u003e $form-\u003eerror()];\n    $ctx-\u003eJSON(200, $data);\n    return;\n}\n\n// 将表单对象直接传递到模型中保存数据\n(new UserModel())-\u003eadd($form);\n```\n\n- `$form-\u003eerror() : string` 获取单条错误信息\n- `$form-\u003eerrors() : array` 获取全部错误信息\n\n## 验证规则\n\n全部的验证类型与对应的验证选项如下\n\n```php\npublic function rules(): array\n{\n    return [\n        'a' =\u003e ['integer', 'unsigned' =\u003e true, 'min' =\u003e 1, 'max' =\u003e 1000000, 'length' =\u003e 10, 'minLength' =\u003e 3, 'maxLength' =\u003e 5],\n        'b' =\u003e ['double', 'unsigned' =\u003e true, 'min' =\u003e 1, 'max' =\u003e 1000000, 'length' =\u003e 10, 'minLength' =\u003e 3, 'maxLength' =\u003e 5],\n        'c' =\u003e ['alpha', 'length' =\u003e 10, 'minLength' =\u003e 3, 'maxLength' =\u003e 5],\n        'd' =\u003e ['alphaNumeric', 'length' =\u003e 10, 'minLength' =\u003e 3, 'maxLength' =\u003e 5],\n        'e' =\u003e ['string', 'length' =\u003e 10, 'minLength' =\u003e 3, 'maxLength' =\u003e 5, 'filter' =\u003e ['trim', 'strip_tags', 'htmlspecialchars']],\n        'f' =\u003e ['email', 'length' =\u003e 10, 'minLength' =\u003e 3, 'maxLength' =\u003e 5],\n        'g' =\u003e ['phone'],\n        'h' =\u003e ['url', 'length' =\u003e 10, 'minLength' =\u003e 3, 'maxLength' =\u003e 5],\n        'i' =\u003e ['in', 'range' =\u003e ['A', 'B'], 'strict' =\u003e true],\n        'j' =\u003e ['date', 'format' =\u003e 'Y-m-d'],\n        'k' =\u003e ['compare', 'compareAttribute' =\u003e 'a'],\n        'l' =\u003e ['match', 'pattern' =\u003e '/^[\\w]{1,30}$/'],\n        'm' =\u003e ['call', 'callback' =\u003e [$this, 'check']],\n        'n' =\u003e ['file', 'mimes' =\u003e ['audio/mp3', 'video/mp4'], 'maxSize' =\u003e 1024 * 1],\n        'r' =\u003e ['image', 'mimes' =\u003e ['image/gif', 'image/jpeg', 'image/png'], 'maxSize' =\u003e 1024 * 1],\n    ];\n}\n```\n\n### call 验证类型\n\n该类型为用户自定义验证规则，callback 内指定一个用户自定义的方法来验证\n\n```php\npublic function check($fieldValue): bool\n{\n    // 验证代码\n    // ...\n    \n    return true;\n}\n```\n\n### file / image 验证类型\n\n该类型用来验证文件，包含的两个验证选项如下：\n\n- mimes：输入你想要限制的文件mime类型，[MIME参考手册](http://www.w3school.com.cn/media/media_mimeref.asp)\n- maxSize：允许的文件最大尺寸，单位 KB\n\n验证成功后模型类会增加一个同名属性，该属性为 Psr\\Http\\Message\\UploadedFileInterface 类的实例化对象，可直接调用 $this-\u003e[attributeName]-\u003emoveTo($targetPath) 移动到你需要存放的位置\n\n## 静态调用\n\n```php\n// 验证是否为字母与数字\nMix\\Validator\\Validate::isAlphaNumeric($value);\n\n// 验证是否为字母\nMix\\Validator\\Validate::isAlpha($value); \n\n// 验证是否为日期\nMix\\Validator\\Validate::isDate($value, $format); \n\n// 验证是否为浮点数\nMix\\Validator\\Validate::isDouble($value); \n\n// 验证是否为邮箱\nMix\\Validator\\Validate::isEmail($value); \n\n// 验证是否为整数\nMix\\Validator\\Validate::isInteger($value);\n\n// 验证是否在某个范围\nMix\\Validator\\Validate::in($value, $range, $strict = false); \n\n// 正则验证\nMix\\Validator\\Validate::match($value, $pattern); \n\n// 验证是否为手机\nMix\\Validator\\Validate::isPhone($value); \n\n// 验证是否为网址\nMix\\Validator\\Validate::isUrl($value); \n```\n\n## License\n\nApache License Version 2.0, http://www.apache.org/licenses/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fvalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmix-php%2Fvalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fvalidator/lists"}