xamin / form
一个具有Agavi集成的表单库。
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-14 12:59:46 UTC
README
这是一组Agavi框架的模型,它可以帮助你创建表单并根据这些模型验证用户输入。
安装
在你的项目中创建一个名为Form的模块,然后将src中的内容放入Form模块的models目录中
$ cd /path/to/your/agavi/project
$ agavi module-create
...
Module name: Form
...
$ cp -r /path/to/agavi-form-models-set/src/* app/modules/Form/models/
还需要将客户端目录中的css/js文件加载到你的页面中。
我已经有一个Form模块了,我该怎么做?
你可以将模型放入现有的Form模块中,但如果存在名称冲突或者你不想这么做,你可以创建一个名为WhatEverYouWant的模块,并在models中将所有Form_
替换为WhatEverYouWant_
$ find /path/to/agavi-form-models-set/src -name "*php" -exec sed -i 's/Form_/WhatEverYouWant_/g' {} \;
如何使用它?
创建一个表单
<?php
// reduce method calls
$context = $this->getContext();
$tm = $context->getTranslationManager();
$form = $context->getModel('Form', 'Form', array(
array(
'id' => 1,
'title' => $tm->_('my new form', 'default.Form'),
'description' => $tm->_('a description/help which will be shown to user', 'default.Form'),
'action' => $this->getContext()->getRouting()->gen(null),
'method' => 'post'
),
null
));
$fieldset = $context->getModel('Elements.Fieldset', 'Form', array(
array(
'id' => 2,
'title' => $tm->_('user info', 'default.Form')
),
$form
));
$firstname = $context->getModel('Elements.TextField', 'Form', array(
array(
'id' => 3,
'title' => $tm->_('first name', 'default.Form'),
'name' => 'firstname',
'required' => true,
'min' => 2,
'max' => 16
),
$fieldset
));
$lastname = $context->getModel('Elements.TextField', 'Form', array(
array(
'id' => 4,
'title' => $tm->_('last name', 'default.Form'),
'name' => 'lastname',
'required' => true,
'min' => 2,
'max' => 32
),
$fieldset
));
$hasEmail = $context->getModel('Elements.Checkbox', 'Form', array(
array(
'id' => 5,
'title' => $tm->_('subscribe?', 'default.Form'),
'name' => 'hasEmail'
),
$fieldset
));
$email = $context->getModel('Elements.TextField', 'Form', array(
array(
'id' => 6,
'title' => $tm->('email', 'default.Form'),
'name' => 'email',
'required' => true,
'parents' => array(
// will be validated if hasEmail has been checked
5 => array(
'opration' => '==',
'condition' => true
)
),
'regex' => '/^[a-z0-9_\.]+@[a-z0-9\.]+\.[a-z]{2,3}$/i',
),
$fieldset
));
$fieldset->addChild($firstname);
$fieldset->addChild($lastname);
$fieldset->addChild($hasEmail);
$fieldset->addChild($email);
$form->addChild($fieldset);
// $form is ready
?>
你也可以使用懒加载配置来创建表单
<?php
// think we have fetched $config from mongodb/couchdb/redis/...
$config = array(
'id' => 1,
'title' => 'our form title',
'description' => 'description of our form',
'items' => array(
array(
'xtype' => 'textfield',
'name' => 'fieldname'
)
)
);
$form = Form_FormModel::fromJson($config);
$form->action = $this->getContext()->getRouting('my.routing');
?>
你说我可以通过表单来验证用户输入,那怎么操作呢?
<?php
// do such thing in register[Method]Validators method of your action
// of course you need to autoload/load Form_ValidatorModel
Form_ValidatorModel::registerValidators(
$yourForm,
$this->getContainer()->getValidationManager(),
$this->getContext()->getRequest()->getRequestData()->getParameters(),
$this->getContext()->getRequest()->getRequestData()->getFiles()
);
?>
如何渲染一个表单?
只需将你的Form_FormModel
实例传递到你的模板/视图中,并像这样渲染它:$form->html()
。
表单的标记是什么?
生成的标记基于twitter-bootstrap。
其他输出类型怎么办?
只支持html,如果你需要其他表示,你必须自己实现。
是否有客户端验证的支持?
是的,目前支持jQueryValidationEngine,当你渲染表单时,将客户端验证作为第一个参数传递,如:$form->html('jQueryValidationEngine')
。
我想使用其他的客户端验证,我该怎么做?
jQueryValidationEngine
是一个元素的方法,html
方法的输出将被传递给它,所以我们改变了html标记以进行客户端验证。你也可以这样做。只需在元素上创建你的验证方法,并让html方法知道它是做什么的。
是否有条件元素的支持?
是的,看看上面的例子,正如你所看到的,如果用户勾选了subscribe?
复选框,电子邮件将会被验证。
我有一个复杂元素,我该如何验证它?
在你的元素上定义registerValidators
方法,你将得到以下参数:AgaviValidationManager $vm, array $depends, array $unvalidatedParameters, array $unvalidatedFiles
,这样你就可以将你的验证器注册到AgaviValidationManager中。
什么是资源元素?
基于jQuery-ui的自动完成来替代html的select元素,因此你可以期待从jQuery-ui自动完成中获得你期望的一切:例如,当你想根据州来加载城市时,例如,当用户选择了纽约作为州时,只显示纽约的城市。