drarok / konform
此软件包的最新版本(dev-master)没有可用的许可信息。
适用于 Kohana 的简单表单生成器,使用 PHPTAL 模板。
dev-master
2013-11-07 20:25 UTC
Requires
- drarok/kotal: dev-develop
This package is auto-updated.
Last update: 2024-08-29 03:27:11 UTC
README
需求
您需要 KOtal 模块,因为视图是用 PHPTAL 编写的,请访问 KOtal。
用法
- 创建 Konform 的子类(例如 Konform_Contact)。
- 在您的子类中设置 $_fields 属性(在其 _init() 方法中或直接在类顶部)。
- 创建一个 messages/konform/contact.php 文件,并填写您的错误信息。
- 实例化您的类并使用它(见下文)。
- 添加一些 CSS 以针对这些表单,然后享受使用。
示例
这是一个联系表单,展示了每个表单元素。
class Konform_Contact extends Konform
{
/**
* Action (location) to submit the form to.
*
* @var string
*/
protected $_action = '/contact';
/**
* Other attributes (name => value) to apply to the form.
*
* @var array
*/
protected $_attributes = array(
'id' => 'something'
'class' => 'konform contact',
);
/**
* Fields, keyed on their name, with label, rules, type, and other options.
*
* @var array
*/
protected $_fields = array(
// Simple text entry box.
'name' => array(
'type' => 'text',
'label' => 'Your name',
'maxlength' => 60,
'required' => true,
'rules' => array('not_empty'),
),
// One with more validation on it, and a custom CSS class applied to the container.
'email' => array(
'type' => 'text',
'label' => 'Your email address',
'required' => true,
'rules' => array('not_empty', 'email'),
'class' => 'email',
),
// Example of a select element.
'group' => array(
'type' => 'select',
'label' => 'Person or group',
'required' => true,
'rules' => array('not_empty'),
'placeholder' => 'Please select...',
'optionSource' => '_getGroups',
'optionValue' => 'pk',
'optionLabel' => 'name',
),
// And a textarea.
'body' => array(
'type' => 'textarea',
'label' => 'Your message',
'required' => true,
'rules' => array('not_empty'),
'cols' => '70',
'rows' => '10',
),
// Don't forget to add a submit button.
'submit' => array(
'type' => 'submit',
'label' => 'Submit',
'class' => 'buttons'
),
);
/**
* Get the groups for the dropdown list.
*
* These source methods must return something iterable (suitable for reading
* by foreach - arrays, Iterators, etc).
*
* @return Database_Result
*/
protected function _getGroups()
{
return ORM::factory('group')->find_all();
}
}
以下是相应的控制器代码。
public function action_index()
{
$contactForm = new Konform_Contact($this->request->post());
if ($this->request->post() && $contactForm->validate()) {
// Looks like we received valid data, so send the emails.
$this->_sendContactEmail($contactForm->data());
HTTP::redirect('/contact/thank-you');
}
$this->response->body($contactForm->render());
}