承接 phpgt/domvalidation 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

phpgt/domvalidation

Composer 安装命令:

composer require phpgt/domvalidation

包简介

Server side form validation using web standards.

README 文档

README

HTML forms can be annotated in such a way that the individual input elements can describe their own validation rules. The simplest annotation is the required attribute, which can be specified on input elements to indicate that the form is not to be submitted until a value is given.

This repository performs W3C form validation for projects that have a server-side DOM, such as within WebEngine applications.

Build status Code quality Code coverage Current version PHP.GT/DomValidation documentation

Example usage

<form id="example-form" method="post">
	<label>
		<span>Your name</span>
		<input name="name" required />
	</label>
	<label>
		<span>Your email</span>
		<input name="email" type="email" required />
	</label>
	<label>
		<span>Your account ID</span>
		<input name="account" pattern="\S*\d{,3}" />
	</label>
	<label>
		<span>Your nation</span>
		<select name="nation" required>
			<option></option>
			<option>Oceania</option>
			<option>Eurasia</option>
			<option>Eastasia</option>
		</select>
	</label>
	<button>Submit</button>
</form>

The above HTML will be validated on the client as usual, but using the PHP below will also provide server-side validation without any additional validation logic to be written.

Validation rules present in the above HTML form:

  • name input is required to be not empty.
  • email input is required to be not empty, and must be a valid email address.
  • account input is not required, but when a value is submitted, it must match the provided regular expression (any number of non-whitespace characters followed by up to 3 numbers).
  • nation input must be one of the three enumerations present in the <select> element.
use GT\Dom\HTMLDocument;
use GT\DomValidation\Validator;
use GT\DomValidation\ValidationException;

// Assume this function is triggered when POST data arrives.
function handleSubmit($inputData) {
	$document = new HTMLDocument(file_get_contents("example-form.html"));
// First, obtain a reference to the form we wish to validate.
	$form = $document->querySelector("#example-form");
	$validator = new Validator();

	try {
// Within a try/catch, pass the form and the user input into the Validator.
		$validator->validate($form, $inputData);
	}
	catch(ValidationException) {
// If there are any validation errors, we can iterate over them to display
// to the page, and return early as to not action the user input.
		foreach($validator->getLastErrorList() as $name => $message) {
// Here we can add an attribute to the parent of the input, for displaying
// the error message using CSS, for example.
			$errorElement = $form->querySelector("[name=$name]");
			$errorElement->parentNode->dataset->validationError = $message;
		}
        
// Return early so user input isn't used when there are validation errors. 
		return;
	}

// Finally, if the input contains no validation errors, continue as usual.
	sendInputToDatabase($inputData);
}

Supported validation mechanisms:

It's possible to add your own validation mechanism by extending the FormValidator class and overriding the necessary functions.

  • required - field can not be left blank
  • pattern - must match the provided regular expression
  • type - must match the provided inbuilt data type
  • min - for numerical inputs, the minimum allowed value
  • max - for numerical inputs, the maximum allowed value
  • minlength - the minimum number of characters allowed
  • maxlength - the maximum number of characters allowed
  • step - the granularity that is required

Supported types:

  • tel
  • url
  • email
  • date
  • month
  • week
  • time
  • datetime-local
  • number
  • range

Special element behaviour

When using <select> and <input type="radio" /> elements, their contained options are used as validation enumerations, meaning that values that are not part of the contained options will throw validation errors.

Proudly sponsored by

JetBrains Open Source sponsorship program

JetBrains logo.

phpgt/domvalidation 适用场景与选型建议

phpgt/domvalidation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 718 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 phpgt/domvalidation 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-30