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.
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:
nameinput is required to be not empty.emailinput is required to be not empty, and must be a valid email address.accountinput 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).nationinput 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 blankpattern- must match the provided regular expressiontype- must match the provided inbuilt data typemin- for numerical inputs, the minimum allowed valuemax- for numerical inputs, the maximum allowed valueminlength- the minimum number of characters allowedmaxlength- the maximum number of characters allowedstep- the granularity that is required
Supported types:
telurlemaildatemonthweektimedatetime-localnumberrange
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
phpgt/domvalidation 适用场景与选型建议
phpgt/domvalidation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 718 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 phpgt/domvalidation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phpgt/domvalidation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 718
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-30