originphp/validation
Composer 安装命令:
composer require originphp/validation
包简介
OriginPHP Validation
README 文档
README
This provides a Validation library and the Validator class for setting up and running validation rules on arrays of data.
Installation
To install this package
$ composer require originphp/validation
Validation Library
For example
$bool = Validation::ip('192.168.1.25');
The full list of validation rules are listed below, after the next section.
Validator
To validate an array of values
$validator = new Validator(); $validator->add('name','required') ->add('email',[ 'optional', // rule name 'email' => [ 'rule' => 'email' // rule name 'message' => 'Invalid email address' // custom message ] ]); $errors = $validator->validate($_POST);
Rule options:
- rule: name of rule, array, callbable e.g. required, numeric, ['date', 'Y-m-d'],[$this,'method']
- message: the error message to show if the rule fails, if not supplied a default one will be used.
- on: default:null. set to create or update to run the rule only on those, works with second argument of
validate - allowEmpty: default:false validation will be pass on empty values
- stopOnFail: default:false wether to continue if validation fails
You can also use custom validation rules using
$validator->add('first_name', 'uniqueRuleName', [ 'rule' => [new NameAssert(),'firstName'] // [$object,'method'] ]);
Or to use a closure
$validator->add('status', 'uniqueRuleName', [ 'rule' => function ($value) { return $value === 'active'; } ]);
The Validator uses the Validation rules and has 3 additional validation rules built in, required, optional, and present.
These rules should be called first, and for required and present, if they fail no further validation rules will run.
- required: this means the key must be
presentand the value isnotEmpty - optional: this means that data is optional, if the value is
emptythen it will not run any more validation rules. - present: this means that the data array must have the key present, its irrelevant if the value is empty or not.
ValidateTrait
You can add validation any Plain Old PHP Object.
class Popo { use ValidateTrait; public $name; public $email; public function __construct() { $this->validate('name','required'); $this->validate('email',[ 'required', 'email' ]); } }
Then you can check its valid
$popo = new Popo(); $popo->name = 'foo'; $popo->email = 'foo@example.com'; if(!$popo->validates()){ $errors = $popo->errors(); }
You can
Validation Rules
accepted
Validates a value is accepted (checkbox is checked)
Validation::accepted($_POST['accepted']);
after
Validates a date is after a certain date, dates are passed to the strtotime function.
Validation::after('2019-01-01','now');
alpha
Validates a string only contains alphabetic characters from the default locale
Validation::alpha('abc');
alphaNumeric
Validates a string only contains alphanumeric characters from the default locale
Validation::alphaNumeric('abc1234');
array
Validates a value is an array
Validation::array([]);
before
Validates a date is before a certain date, dates are passed to the strtotime function.
Validation::before('2019-01-01','today');
boolean
Validates a value is a boolean type
Validation::boolean(true);
creditCard
Validates a credit card number
All regex rules written from scratch using current IIN ranges, so whilst they are accurate the rules are not mature yet.
Validation::creditCard('2222 9909 0525 7051');
date
Validates a date using a format compatible with the PHP DateTime class.
Validation::date('2019-01-01'); Validation::date('01/01/2019','d/m/Y');
dateFormat
Validates a date using a format compatible with the PHP DateTime class, this is used by Validation::date, Validation::time, and Validation::dateTime.
The format, which is the second argument is required
Validation::dateFormat('01/01/2019','d/m/Y');
dateTime
Validates a datetime using a format compatible with the PHP DateTime class.
Validation::dateTime('2019-01-01 17:23:00'); Validation::dateTime('01/01/2019 17:23','d/m/Y H:i');
decimal
Validates a value is a float. Alias for float
Validation::decimal(0.007); Validation::decimal('0.007');
Validation for an email address
Validation::email('foo@example.com');
You can also check that the email address has valid MX records using the getmxrr function.
Validation::email('foo@example.com',true);
equalTo
Validates a value is equal to another value, only values are compared.
Validation::equalTo(5,5); Validation::equalTo('5',5);
extension
Validates a value has an extension. If an array is supplied it will look
Validation::extension('filename.jpg',['jpg','gif']);
You can also check a file that has been uploaded with the correct extension.
Validation::extension($_FILES['file1'],['jpg','gif']);
float
Validates a value is a float.
Validation::float(0.007); Validation::float('0.007');
fqdn
Validates a string is Fully Qualified Domain Name (FQDN).
Validation::fqdn('www.originphp.com');
You can also check the DNS records using checkdnsrr to ensure that is really valid and not just looks like its valid.
Validation::fqdn('www.originphp.com',true);
greaterThan
Validates a value is greater than
Validation::greaterThan(4,1);
greaterThanOrEqual
Validates a value is greater than or equals a value.
Validation::greaterThanOrEqual(4,1);
hex
Validates a hexadecimal string.
Validation::hex('b1816172fd2ba98f3af520ef572e3a47');
hexColor
Validates a value is a hex color
Validation::hexColor('#f5f5f5');
iban
Validates a value is an IBAN number
Validation::iban('DE89 3704 0044 0532 0130 00');
in
Validates a value is in a list
Validation::in('foo',['foo','bar']);
integer
Validates a value is an integer
Validation::integer('1'); Validation::integer(1);
ip
Validates a value is an IP Address, by default it validates as either IPV4 or IPV6.
Validation::ip('192.168.1.1');
To validate only IPV4 or IPV6
Validation::ip('192.168.1.10','ipv4'); Validation::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334','ipv6');
ipRange
Validates an IP address is in a range.
Validation::ipRange('192.168.1.5','192.168.168.1','192.168.1.10');
json
Validates a value is a JSON string.
$data = json_encode('foo'); Validation::json($data);
length
Validates a string has a certain length.
Validation::length('foo', 3);
lessThan
Validates a value is less than another a value.
Validation::lessThan(3,5);
lessThanOrEqual
Validates a value is less than or equal to another a value.
Validation::lessThanOrEqual(5,5);
lowercase
Validates a string is in lowercase.
Validation::lowercase('foo');
luan
Validates a number using the LUAN algoritm.
Validation::luan('7992739871');
macAddress
Validates a string is a valid mac address.
Validation::macAddress('00:0b:95:9d:00:17');
maxLength
Validates a string has a maximum length.
Validation::maxLength('foo',3);
md5
Validates a string is a MD5 hash.
Validation::md5('b1816172fd2ba98f3af520ef572e3a47');
You can also allow it to be case insensitive
Validation::md5('B1816172FD2BA98F3AF520EF572E3A47',true);
mimeType
Validates a file has a particular mime type.
Validation::mimeType('phpunit.xml','text/xml'); Validation::mimeType('import.csv',['text/csv''text/plain']); Validation::mimeType($_FILES['upload1'],'application/pdf');
minLength
Validates a string has a minimum length.
Validation::minLength('foo',3);
notBlank
Validates a value is not empty and has anything other than whitespaces.
Validation::notBlank('foo');
notEmpty
Validates a value is not empty, a value is empty
null- empty string
'' - an empty array
- empty file upload
Validation::notEmpty('foo');
notIn
Validates a value is not in an array of values.
Validation::notIn('fooz',['foo','bar']);
numeric
Validates a value is an integer or a float.
Validation::numeric('1'); Validation::numeric(1); Validation::numeric(9.99); Validation::numeric('9.99');
present
Validates an array has a key.
$data = ['foo'=>'bar']; Validation::present($data,'foo');
range
Validates a number or float is within a range.
Validation::range(5,1,10); Validation::range('5',1,10);
regex
Validates a string using a REGEX pattern.
Validation::regex('foo','/foo/');
string
Validates that a value is a string
Validation::string('foo);
time
Validates a string is a time.
Validation::time('10:20'); Validation::time('10:20:00','H:i:s');
upload
Validates a file upload was successful.
Validation::upload($_FILES['upload1']); Validation::upload($_FILES['upload1']['error']);
You can also allow optional file upload, meaning if no file is upload then it will return true if there was no error uploading it.
Validation::upload($_FILES['upload1'],true);
uppercase
Validates a string is uppercase.
Validation::uppercase('FOO');
url
Validates a string is an URL.
Validation::url('www.example.com/q=foo'); Validation::url('http://www.google.com', true);
uuid
Validates a string is a UUID
Validation::uuid('10458466-a809-4e7a-b784-68d78c25d092');
You can also allow uppercase
Validation::uuid('86E6E3FC-4924-4B5F-8BCA-E4C07F7CDDF9',true);
originphp/validation 适用场景与选型建议
originphp/validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.21k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 12 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「validate」 「IBAN」 「credit card」 「luhn」 「MD5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 originphp/validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 originphp/validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 originphp/validation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
This package parses and validates International Bank Account Number (IBAN).
PHP IBAN - This library provides a converter for account number, bank code and IBAN.
Supercharged text field validation.
Extensions for Laravel validation rules to validate national person and company identifiers
Integration bundle for Aura.Input with Aura.Filter
统计信息
- 总下载量: 10.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 23
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-10