flsouto/htfield
Composer 安装命令:
composer require flsouto/htfield
包简介
Base class for implementing all kinds of form fields, both widgets and non-widgets
README 文档
README
Overview
The HtField class is a base class for implementing all kinds of form fields, both widgets and non-widgets. It is basically a wrapper for two things:
- parameter resolution
- tag attribute setting
For understanding these concepts more deeply I recommend you take a look at the two underlying libraries being used by this class:
Installation
Install this library via composer
composer require flsouto/htfield
Usage
As this is an abstract class, it is only useful if you want to build your own form field types. Therefore in this document I will show you how you can extend it in order to implement a very simple widget.
Defining a Simple Widget
Our example widget is going to be poor in functionality but will be enough for demonstrating the base API. We are going to call it 'MyField'. See its definition below:
<?php use FlSouto\HtField; require_once('vendor/autoload.php'); class MyField extends HtField{ // Set field as required function required($errmsg){ $this->param->filters()->required($errmsg); return $this; } // It is mandatory to implement the 'render' method function render(){ echo "<input ".$this->attrs." />"; } }
Rendering the Field
To render a field instance simply print it out:
<?php require_once('vendor/autoload.php'); $field = new MyField('email'); echo $field;
The output will be:
<input id="field_5918f4b6a4df9" name="email" />
Changing the ID
By default, all fields have a default, random id. You can change it to a custom string by calling the attributes API. See example below:
$field = new MyField('email'); $field->attrs(['id'=>'email_field']); echo $field;
The output will be:
<input id="email_field" name="email" />
Retrieving the ID
Use the $field->id() method for that:
$field = new MyField('email'); $field->attrs(['id'=>'email_field']); echo $field->id();
Outputs:
email_field
The name attribute
The name is a special attribute that can only be set once via constructor. However you can access it's value later by using the $field->name() getter:
$field = new MyField('address'); echo $field->name();
Outputs:
address
Processing Input
The $field->process() method returns an object which contains two properties:
- $field->output: contains the value extracted from the specified source of input
- $field->error: contains any error messages occurred during the extraction process
But, before you can process anything you must specify the source of input. In other words, you must set the "context" from which the data is to be extracted. See example:
$field = new MyField('username'); $field->context(['username'=>'Jack']); echo $field->process()->output;
Outputs:
Jack
Observations
- The context method can be chained just like any other setter method of this class.
- The process method accepts an optional context array, which, if provided, will be used instead of the one set by
$field->context().
Processing Namespaced Fields
A field can have a fully qualified name in the following form:
path[to][field_name]
This means that the input is expected to be found in a data structure like this:
Array(
[path] => Array(
[to] => Array(
[field_name] => INPUT
)
)
)
Here is an example of this concept in action:
$field = new MyField('user[contact][email]'); $field->context(['user'=>['contact'=>['email'=>'user@domain.com']]]); echo $field->process()->output;
Output:
user@domain.com
The $field->value() shortcut
Instead of writing the rather verbose statement $field->process()->output you can simply call $field->value() which has the same effect:
$field = new MyField('description'); $input = $field ->context(['description'=>'This is just a test']) ->value(); echo $input;
Results in:
This is just a test
The $field->validate() shortcut
This is an alias to $field->process()->error:
$field = new MyField('name'); $error = $field->required("Provide a name!") ->context(['name'=>'']) ->validate(); echo $error;
Provide a name!
flsouto/htfield 适用场景与选型建议
flsouto/htfield 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 203 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 01 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「html」 「Forms」 「fields」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 flsouto/htfield 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 flsouto/htfield 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 flsouto/htfield 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
ConfirmationField is a form field for Atk14 applications. It's like the BooleanField (checkbox) but the ConfirmationField must be ticked.
Field for number with restricted count of digits and decimal places
Register advanced custom fields with object oriented PHP
HTML and form generation
Build forms from schema
统计信息
- 总下载量: 203
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2017-01-21