{"id":33944694,"url":"https://github.com/crazymus/pvalidate","last_synced_at":"2026-04-11T05:02:06.531Z","repository":{"id":56958790,"uuid":"184844489","full_name":"crazymus/pvalidate","owner":"crazymus","description":"简单易用，可扩展的php数据验证器","archived":false,"fork":false,"pushed_at":"2019-08-10T09:21:19.000Z","size":71,"stargazers_count":2,"open_issues_count":11,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2026-01-03T07:46:01.476Z","etag":null,"topics":["php","validation-plugin"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crazymus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-04T02:42:45.000Z","updated_at":"2019-08-10T09:21:22.000Z","dependencies_parsed_at":"2022-08-21T09:50:28.627Z","dependency_job_id":null,"html_url":"https://github.com/crazymus/pvalidate","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/crazymus/pvalidate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crazymus%2Fpvalidate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crazymus%2Fpvalidate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crazymus%2Fpvalidate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crazymus%2Fpvalidate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crazymus","download_url":"https://codeload.github.com/crazymus/pvalidate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crazymus%2Fpvalidate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31669117,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-10T17:19:37.612Z","status":"online","status_checked_at":"2026-04-11T02:00:05.776Z","response_time":54,"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":["php","validation-plugin"],"created_at":"2025-12-12T17:21:28.401Z","updated_at":"2026-04-11T05:02:06.519Z","avatar_url":"https://github.com/crazymus.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ccenter\u003e\n    \u003cimg src=\"https://crazymus.oss-cn-beijing.aliyuncs.com/1111.png\" style=\"width:500px;\"/\u003e\n\u003c/center\u003e\n\n[![Travis (.org)](https://img.shields.io/travis/crazymus/pvalidate.svg)](https://www.travis-ci.org/crazymus/pvalidate)\n[![GitHub](https://img.shields.io/github/license/crazymus/pvalidate.svg)](LICENSE)\n![GitHub repo size](https://img.shields.io/github/repo-size/crazymus/pvalidate.svg)\n![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/crazymus/pvalidate/v2.0.svg)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/cbb59d5397d64366b7a8f05266c0aeb2)](https://www.codacy.com/app/crazymus/pvalidate?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=crazymus/pvalidate\u0026amp;utm_campaign=Badge_Grade)\n\n数据校验是web开发中重要的环节，也是一项很繁琐的工作。为了提升效率，我想到开发一款通过简单的配置就能完成数据校验的工具，于是，Pvalidate诞生了.\n\n## 使用要求 ##\n-  \\\u003e= PHP5.3\n-  mbstring扩展\n\n## 安装 ##\n项目目录下执行\n```\ncomposer require \"crazymus/pvalidate\"\n```\n若没有全局安装composer，可执行\n```\nphp composer.phar require \"crazymus/pvalidate\"\n```\n若无法正常安装，推荐使用composer中国全量镜像\n- https://pkg.phpcomposer.com\n\n## 目录 ##\n- [字符串校验](#字符串校验)\n- [数字校验](#数字校验)\n- [整数校验](#整数校验)\n- [浮点数校验](#浮点数校验)\n- [手机号校验](#手机号校验)\n- [金额校验](#金额校验)\n- [邮箱校验](#邮箱校验)\n- [URL校验](#URL校验)\n- [身份证校验](#身份证校验)\n- [枚举值校验](#枚举值校验)\n- [自定义校验规则](#自定义校验规则)\n- [自定义错误信息](#自定义错误信息)\n\n## 字符串校验 ##  \n```php\n\u003c?php \n\n$params = array(\n    'name' =\u003e 'crazymus',\n    'age' =\u003e 20\n);\n\n$rules = array(\n    'name' =\u003e array(\n        'type' =\u003e 'string', // 字符串类型\n        'title' =\u003e '姓名', // 字段名称\n        'required' =\u003e true, // 必填 \n        'length' =\u003e 10, // 长度必须等于10\n        'charset' =\u003e 'GBK', // 字符串编码，所不指定则默认使用文档的编码\n    )\n);\n\n// 长度支持运算符 \n$rules = array(\n    'name' =\u003e array(\n        'type' =\u003e 'string',  \n        'length' =\u003e array('\u003e', 10), // 长度必须大于10  \n    )\n);\n\n$rules = array(\n    'name' =\u003e array(\n        'type' =\u003e 'string',  \n        'length' =\u003e array( // 长度必须大于10，且小于等于50\n            array('\u003e', 10),\n            array('\u003c=', 50)\n        ), \n    )\n);\n\ntry {\n    $validateParams = \\Crazymus\\Pvalidate::validate($params, $rules);\n    print_r($validateParams);  // 校验后得到的数据\n} catch (\\Exception $e) {\n    $e-\u003egetMessage();\n}\n```\n[返回目录](#目录)\n \n## 数字校验 ## \n```php\n\u003c?php\n\n$rules = array(\n    'age' =\u003e array(\n        'type' =\u003e 'number', \n        'title' =\u003e '年龄',\n        'required' =\u003e true,\n        'value' =\u003e 10, // 值必须等于10\n    ), \n);\n\n// 支持运算符校验\n$rules = array(\n    'age' =\u003e array(\n        'type' =\u003e 'number', \n        'value' =\u003e array('\u003e', 18) // 大于18\n    )\n);\n\n$rules = array(\n    'age' =\u003e array(\n        'type' =\u003e 'number', \n        'value' =\u003e array(  // 大于等于18，且小于60 \n            array('\u003e=', 18),\n            array('\u003c', 60)\n        )\n    )\n);\n\n?\u003e\n```\n[返回目录](#目录)\n\n## 整数校验  ##\n```php\n\u003c?php\n\n$rules = array(\n    'age' =\u003e array(\n        'type' =\u003e 'integer', \n        'title' =\u003e '年龄',\n        'required' =\u003e true,\n        'value' =\u003e array('\u003e', 18)\n    ), \n);\n\n?\u003e\n```\n[返回目录](#目录)\n\n## 浮点数校验 ##\n```php\n\u003c?php\n\n$rules = array(\n    'ratio' =\u003e array(\n        'type' =\u003e 'float', \n        'title' =\u003e '比率',\n        'required' =\u003e true,\n        'precision' =\u003e 2, // 小数点位数最大为2 \n        'value' =\u003e array('\u003e', 1.25)\n    ), \n);\n\n?\u003e\n```\n[返回目录](#目录)\n\n## 手机号校验 ##\n```php\n\u003c?php\n\n$rules = array(\n    'phone' =\u003e array(\n        'type' =\u003e 'phone', \n        'title' =\u003e '手机号',\n        'required' =\u003e true,\n    ),\n);\n\n?\u003e\n```\n[返回目录](#目录)\n\n## 金额校验 ##\n```php\n\u003c?php\n\n$rules = array(\n    'money' =\u003e array(\n        'type' =\u003e 'money', \n        'title' =\u003e '金额',\n        'required' =\u003e true,\n    ),\n);\n\n?\u003e\n```\n[返回目录](#目录)\n\n## 邮箱校验 ##\n```php\n\u003c?php\n\n$rules = array(\n    'email' =\u003e array(\n        'type' =\u003e 'email', \n        'title' =\u003e '邮箱',\n        'required' =\u003e true,\n    ),\n);\n\n?\u003e\n```\n[返回目录](#目录)\n\n## URL校验 ##\n```php\n\u003c?php\n\n$rules = array(\n    'site' =\u003e array(\n        'type' =\u003e 'url', \n        'title' =\u003e '网址',\n        'required' =\u003e true,\n    ),\n);\n\n?\u003e\n```\n[返回目录](#目录)\n\n## 身份证校验 ##\n```php\n\u003c?php\n\n$rules = array(\n    'idcard' =\u003e array(\n        'type' =\u003e 'idCard', \n        'title' =\u003e '身份证',\n        'required' =\u003e true,\n    ),\n);\n\n?\u003e\n```\n[返回目录](#目录)\n\n## 枚举值校验 ##\n```php\n\u003c?php \n\n$rules = array(\n    'sex' =\u003e array(\n        'type' =\u003e 'number', \n        'title' =\u003e '性别',\n        'required' =\u003e true,\n        'enum' =\u003e array(1, 2),  // 必须是数组中的值\n    ),\n)\n\n?\u003e\n```\n[返回目录](#目录)\n\n## 自定义校验规则 ##\n我们可以编写自己的校验规则，来应对特殊的校验场景。可以根据参数的含义，选择继承StringRule\n或者NumberRule，并重载validate方法即可。\n```php\n\u003c?php namespace MyApp\\Rules;\n\nclass MyRule extends \\Crazymus\\Rule\\StringRule\n{\n    public function validate($value)\n    {\n        parent::validate($value);\n\n        //TODO 实现你的校验逻辑\n        if (!in_array($value, array('music', 'movie', 'book'))) {\n            throw new \\Crazymus\\PvalidateException($this-\u003erenderErrorMsg('参数校验失败'));\n        }\n    }\n}\n\n// 添加自定义规则 \n\\Crazymus\\Pvalidate::addRules(array(\n    'myRule' =\u003e '\\MyApp\\Rules\\MyRule'\n));\n\n// 使用自定义规则 \n$rules = array(\n    'hobby' =\u003e array(\n        'type' =\u003e 'myRule', \n        'title' =\u003e '爱好',\n        'required' =\u003e true,\n    )\n);\n\n?\u003e\n```\n[返回目录](#目录)\n\n## 自定义错误信息 ##\n```php\n\u003c?php \n\n$rules = array(\n    'name' =\u003e array(\n        'type' =\u003e 'string',\n        'title' =\u003e '姓名',\n        'required' =\u003e true,\n        'errorMsg' =\u003e '姓名格式错误', // 会覆盖默认错误提示\n    ),\n)\n\n?\u003e\n```\n[返回目录](#目录)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrazymus%2Fpvalidate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrazymus%2Fpvalidate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrazymus%2Fpvalidate/lists"}