承接 flsouto/htfield 相关项目开发

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

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

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-01-21