quantumunit / php-validation
PHP 验证库
Requires
- monolog/monolog: 2.3.*
- symfony/yaml: ^3.0 || ^4.0 || ^5.0
Requires (Dev)
- phpunit/phpunit: 9.5.*
This package is auto-updated.
Last update: 2024-09-17 11:27:26 UTC
README
我在2007年首次学习PHP。于2014年重写,使用YAML而不是XML。从命令链模式更改为基本按需加载。
用法
$loader = new YamlConfiguration();
$loader->loadConfig(__SITE_PATH . '/validation-config.yml');
//YAML key $key = 'new_user_signup'; $validator = new Validator($loader, $this->getLogger()); //Monolog\Logger
$result = $validator->validateRequest($key, $this->getPostedParams());
注意
如果$result是一个包含值的数组,它将是一个关联数组,其中字段名是索引键,LANG_STRING(用于多语言支持)是索引的值。
sample YAML config (validation-config.yml)
failkey: some_other_yml_key
fields
#uses 2 validation routines in this example, Required then String - firstname: - class: Required
failkey: VALIDATION_REQUIRED_FIELD - class: String
failkey: VALIDATION_INVALID_STRING params: maxlength: 20
- lastname:
-
class: Required
failkey: VALIDATION_REQUIRED_FIELD
-
class: String
failkey: VALIDATION_INVALID_STRING
params:
maxlength: 20
#email is optional in this sample so no Required validation needed - email - class: Email failkey: VALIDATION_INVALID_EMAIL
可能的验证类
Address
AlphaNumeric - letters and numbers only
Alphabet - letters only
BusinessName - letters, numbers, apostrophe, and &()-,.
Currency
Date
Email
IPAddress
Integer
Required
String - letters, spaces and apostrophes
Telephone
URL