zeuxisoo/core-validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zeuxisoo/core-validator

Composer 安装命令:

composer require zeuxisoo/core-validator

包简介

PHP validator for form data

README 文档

README

Status

Build Status

Installation

Install the composer

curl -sS https://getcomposer.org/installer | php

Edit composer.json

{
	"require": {
		"zeuxisoo/core-validator": "dev-master"
	}
}

Install/update your dependencies

php composer.phar install

Usage

Load the validation library

use \Zeuxisoo\Core\Validator;

Initial the Validator object by $_POST

$validator = Validator::factory($_POST);

The example of $_POST like:

$_POST = array(
	'username' => "",
	'password' => "123456",
	'confirm_password' => "125",
	'first_name' => "123",
	'telephone' => '123456789',
	'email' => "abc@abc",
	'emails' => "abc@abc.com,abc@abc.com,abc",
	'url' => "http://127.0.0.1/()",
	'ip' => '127.0.0.1a',
	'title' => '123a',
	'age' => 17,
	'custom' => "2001-12",
	'gender' => 'boy',
	'zip_code' => 1002,
);

Then, add the target keys/fields and checking rules

$validator = Validator::factory($data);
$validator->add('username', 'Pleas enter username')->rule('required');
$validator->add('username', 'The username must less than 4 char')->rule('min_length', 4);	// empty so not show
$validator->add('password', 'Password not match input value')->rule('match_value', $data['confirm_password']);
$validator->add('first_name', 'The first name not match given pattern')->rule('match_pattern', '/^[A-Za-z]/');
$validator->add('password', 'Password not match given field')->rule('match_field', 'confirm_password');
$validator->add('confirm_password', 'Confirm password must more than 4 char')->rule('min_length', 4);
$validator->add('telephone', 'The telephone number must less than 8 char')->rule('max_length', 8);
$validator->add('telephone', 'The telephone number length must equals 8 char')->rule('exact_length', 8);
$validator->add('email', 'Invalid email address')->rule('valid_email');
$validator->add('url', 'Invalid email address')->rule('valid_url');
$validator->add('ip', 'Invalid ip address')->rule('valid_ip');
$validator->add('title', 'The string is not alpha string')->rule('valid_string');
$validator->add('age', 'The age must less than 18 age')->rule('numeric_min', 18);
$validator->add('age', 'The age must bigger than 16 age')->rule('numeric_max', 16);
$validator->add('custom', 'The value is not much the custom format')->rule('custom', function($val) {
	return preg_match('/2001\-10/', $val) > 0;
});
$validator->add('name', 'The name is equals to Tomcat')->rule('is_true', function($val) {
	return $val === "Tomcat";
});
$validator->add('name', 'The name is not equals to Cattom')->rule('is_false', function($val) {
	return $val === "Cattom";
});
$validator->add('gender', "The gender is supported")->rule('between', array('boy', 'girl'));
$validator->add('zip_code', 'The zip code is not exists')->rule('key_exists', array(
	1002 => "B",
	1003 => "C",
	1004 => "D",
));

But, you can make it chainable like

$validator->add('username', 'Pleas enter username')->rule('required')
		  ->add('username', 'The username must less than 4 char')->rule('min_length', 4)
		  ->add('password', 'Password not match input value')->rule('match_value', $data['confirm_password'])
		  ->add('first_name', 'The first name not match given pattern')->rule('match_pattern', '/^[A-Za-z]/')
		  ->add('password', 'Password not match given field')->rule('match_field', 'confirm_password')
		  ->add('confirm_password', 'Confirm password must more than 4 char')->rule('min_length', 4)
		  ->add('telephone', 'The telephone number must less than 8 char')->rule('max_length', 8)
		  ->add('telephone', 'The telephone number length must equals 8 char')->rule('exact_length', 8)
		  ->add('email', 'Invalid email address')->rule('valid_email')
		  ->add('url', 'Invalid email address')->rule('valid_url')
		  ->add('ip', 'Invalid ip address')->rule('valid_ip')
		  ->add('title', 'The string is not alpha string')->rule('valid_string')
		  ->add('age', 'The age must less than 18 age')->rule('numeric_min', 18)
		  ->add('age', 'The age must bigger than 16 age')->rule('numeric_max', 16)
		  ->add('custom', 'The value is not much the custom format')->rule('custom', function($val) {
				return preg_match('/2001\-10/', $val) > 0;
			})->rule('valid_string', array('utf8', 'alpha', 'lowercase'))
		  ->add('name', 'The name is equals to Tomcat')->rule('is_true', function($val) {
		  		return $val === "Tomcat";
			})
		  ->add('name', 'The name is not equals to Cattom')->rule('is_false', function($val) {
				return $val === "Cattom";
			})
		  ->add('gender', "The gender is supported")->rule('between', array('boy', 'girl'));
		  ->add('zip_code', 'The zip code is not exists')->rule('key_exists', array(
				1002 => "B",
				1003 => "C",
				1004 => "D",
		  ));

Finally, when you added rules completely, you can run the checking.

if ($validator->inValid() === true) {
	foreach($validator->errors() as $error) {
		echo "<p>",$error,"</p>";
	}
}

The method $validator->inValid() or alias method $validator->valid() will return the status of validation (true is contain error), the $validator->error() will contain all error messages. if you just want to show first error message, you can use method: $validator->firstError()

if ($validator->inValid() === true) {
	echo $validator->firstError(); // pop first error
}

All avaliable method:

required
min_length, 100
match_value, "string"
match_pattern, /regex/
match_field, "post_field"
max_length, 100
exact_length, 8               (equals length)
valid_email
valid_url
valid_ip
valid_string
numeric_min, 18
numeric_max, 16
custom, callback
is_true, callback             (callback result is true)
is_false, callback            (callback result is false)
between, array('HK', 'TW')
key_exists, array(1 => 'A',)

zeuxisoo/core-validator 适用场景与选型建议

zeuxisoo/core-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 357 次下载、GitHub Stars 达 2, 最近一次更新时间为 2013 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「form」 「validator」 「validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 zeuxisoo/core-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 zeuxisoo/core-validator 我们能提供哪些服务?
定制开发 / 二次开发

基于 zeuxisoo/core-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 357
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2013-06-24