germania-kg/formvalidator
Composer 安装命令:
composer require germania-kg/formvalidator
包简介
Callable for validating and filtering user inputs with convenient evaluation API.
README 文档
README
Callable for validating and filtering user inputs with convenient evaluation API.
Installation with Composer
$ composer require germania-kg/formvalidator
Form validation
The validation is done by invoking the object. The __invoke method accepts both Arrays and Psr\Http\Message\ServerRequestInterface instances.
<?php use Germania\FormValidator\FormValidator; use Germania\FormValidator\InputContainer; // Setup $required = [ "send_button" => FILTER_VALIDATE_BOOLEAN, "user_email" => FILTER_VALIDATE_EMAIL, "user_login_name" => FILTER_SANITIZE_STRING ]; $optional = [ "family_name" => FILTER_SANITIZE_STRING, "first_name" => FILTER_SANITIZE_STRING ]; $formtest = new FormValidator( $required, $optional ); // Invoking uses PHP's filter_var_array internally. // Arrays accepted but Psr\Http\Message\ServerRequestInterface // will do as well. // Return value is InputContainer instance: $filtered_input = $formtest( $_POST ); $filtered_input = $formtest( $request ); // At least one required field valid? echo $formtest->isSubmitted(); // All required fields valid? echo $formtest->isValid();
Adding fields
After instantiation, you can add required or optional fields. An existing optional field is no longer optional if added using addRequired, the same goes with required fields, if added using addOptional .
$formtest = new FormValidator( $required, $optional ); $formtest->addRequired('additional_info', FILTER_SANITIZE_STRING); $formtest->addOptional('additional_info', FILTER_SANITIZE_STRING);
Removing fields
After instantiation, you can remove required or optional fields.
$formtest = new FormValidator( $required, $optional ); $formtest->removeRequired('user_email'); $formtest->removeOptional('family_name');
Filtered Result: InputContainer
The InputContainer is a PSR-11 Container and also implements ArrayAccess.
ArrayAccess
<?php // Invocation returns InputContainer instance $filtered_input = $formtest( $_POST ); // ArrayAccess: // If field not set, return values are null. echo $filtered_input['foo']; echo $filtered_input->offsetGet('foo');
Simple Array access
<?php // Invocation returns InputContainer instance $filtered_input = $formtest( $_POST ); $input_array = $filtered_input->getArrayCopy();
ContainerInterface
<?php use Germania\FormValidator\NotFoundException; use Psr\Container\NotFoundExceptionInterface; // Invocation returns InputContainer instance $filtered_input = $formtest( $_POST ); try { echo $filtered_input->has('foo'); echo $filtered_input->get('foo'); } catch (NotFoundException $e) { // not found } catch (NotFoundExceptionInterface $e) { // not found }
Filtered Result: Custom InputContainer
The FormValidator class optionally accepts a Callable that takes the filtered input. It should return something useful (such as the default InputContainer).
Variant A: Using the constructor
// Setup the factory $factory = function( $filtered_input ) { return new \ArrayObject( $filtered_input ); }; // Pass to the ctor $formtest = new FormValidator( $required, $optional, $factory ); // Returns an ArrayObject $filtered_input = $formtest( $_POST );
Variant B: Use per call
// Setup as usual: $formtest = new FormValidator( $required, $optional ); $filtered_input = $formtest( $_POST ); // While the above returns the usual InputContainer, // this will return an ArrayObject: $filtered_input = $formtest( $_POST, function( $filtered_input ) { return new \ArrayObject( $filtered_input ); });
Issues
See issues list.
Development
$ git clone https://github.com/GermaniaKG/FormValidator.git
$ cd FormValidator
$ composer install
Unit tests
Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:
$ composer test # or $ vendor/bin/phpunit
germania-kg/formvalidator 适用场景与选型建议
germania-kg/formvalidator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 135 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 germania-kg/formvalidator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 germania-kg/formvalidator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 135
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-01-11