ezformz/ezformz
Composer 安装命令:
composer require ezformz/ezformz
包简介
Form builder and validation for PHP 5.3+
README 文档
README
Basic Usage
After including ezformz.php, you can get an instance either by:
- Using the new operator (ie new EzFormz;)
- Delegating construction of the object (such as CodeIgniter does when creating pseudo-singletons for libraries, ie $this->ezformz) and then calling the instance() method in object scope to create a new (optionally named) instance
- Using the instanceStatic method to create a new (optionally named) instance
You might want to use named instances if you are embedding multiple forms on a page, if you are reusing partials, or just for organizational / clarity reasons.
$form = EzFormz::instanceStatic('optional-instance-name')
->open()
->text('my_field', array('label' => 'My Field', 'rules' => 'required|numeric'))
->submit('submit', array('label' => 'Submit'))
->close();
EzFormz provides a one:one relationship between the HTML form fields and their arguments you already know and the methods used to generate them:
->text('name', array('label' => 'Your Name'))
->select(
'gender',
array(
'label' => 'Your Gender',
'options' => array(
'M' => 'Male',
'F' => 'Female'
)
)
)
->checkbox(
'newsletter',
array(
'label' => 'Sign up for newsletter?',
'value' => 1
)
)
->textarea(
'comments'
array(
'label' => 'Your comments'
)
)
->file(
'upload'
array(
'label' => 'Your file'
)
)
Multi-Methods
If you want multiple form elements to display on the same line, you can use a multi method. The only difference in how your individual elements are constructed are that instead of being method calls in themselves, they are passed as arrays into the multi-method.
->multi(
'text',
array(
array('first_name', array('label' => 'First name')),
array('last_name', array('label' => 'Last name'))
)
)
Rules and Callbacks
EzFormz supports both validation rules as well as validation callbacks. Validation rules are intended as simple rules like this:
'rules' => 'required|is_domain|matches[pattern]'
Each rule is separated by pipes. Arguments are passed within []. You can see a full list of available validator functions in the class file. They are prepended with validator.
You can also pass callbacks. Callbacks work by providing a function, along with an array of arguments and an assertion. If the assertion fails, the error message you provide will be added to the errors propery and validation will fail. You can pass closures (this is an easy way to add special validation methods in an ad-hoc way), or pass objects. Args are passed as an array, but you can decide if you want your function to receive it as a single array argument or as an argument list. Here is an example of how one might verify a user does not yet exist:
$form
->open()
->text(
'email',
array(
'label' => 'Email',
'rules' => 'required',
'rules_callback' => array(
array(
'object' => $model_user,
'function' => 'user',
'args' => array('email' => $i->post('email')),
'args_as_list' => false,
'assert' => false,
'error' => 'User with email '.$i->post('email').' is already registered.'
)
)
)
)
...other fields
->submit()
->close();
Validation and Errors
You can retrieve errors either from the public error property of the form object, using the error_list() method (which generates an unordered list of errors) or with error_json().
if(//form posted){
if(!$form->validate()){
$error_list = $form->error_list();
//Send $error_list to your view
...
}
else
{
//Do ya thang homie
}
}
Displaying Your Form
echo $form;
when you're ready to display your form you can simply echo the form (or assign it to another variable and echo that). It uses PHP's __toString magic method to display the form.
Conclusion
Obviously, EzFormz is about chaining methods easily together and having a standard, expected interface for working with fields. The name of each fieldalways corresponds to the html form element it is producing (select, textarea, text, checkbox, etc.). The first argument is always the name of the field and the second is always an array of options. Some options, like label and rules, are special. Most are never defined in the library itself and will arbitrarily be added to your html elements (so you can add class, id, ref, custom things for javascript, whatever).
EzFormz is still in an early stage and only a few validation methods exist (though regex validation is available, so you could conceivably do anything you wanted), but I think this is a great interface for working with forms and hope you'll agree!
ezformz/ezformz 适用场景与选型建议
ezformz/ezformz 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 93 次下载、GitHub Stars 达 1, 最近一次更新时间为 2012 年 12 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「form」 「Forms」 「builder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ezformz/ezformz 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ezformz/ezformz 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ezformz/ezformz 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
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
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
A Sphinx Query Builder extension for the Laravel Database package
Anax Database Active Record module for model classes.
HTML and form generation
统计信息
- 总下载量: 93
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-12-29