Zend Framework表单与jQuery [英] Zend Framework form with jquery
问题描述
有人知道如何用Zend_Form和jquery简单地创建一个表单吗?我想使用Zend_Form来验证表单,因此不必在JavaScript和PHP中对脚本进行双重编写.
Any one a idea how to simply create a form with Zend_Form and jquery? I want to use Zend_Form to validate the form so I don't have to dual script the form in JavaScript and PHP.
谢谢
Ivo Trompert
Ivo Trompert
推荐答案
那里没问题.
如果使用自动加载功能,则将ZendX_JQuery添加到您的库中.
Add ZendX_JQuery to your library if you use autoload.
然后根据您的需要扩展ZendX_JQuery_Form. 在类的init()方法中完成工作.
Then extend ZendX_JQuery_Form to your needs. Do your stuff in the init() method of your class.
例如,我能够创建一个具有常规Zend_Form验证以及如下JQuery行为的AutoComplete字段:
For example, I was able to create an AutoComplete field which has regular Zend_Form validation plus JQuery behavior like this:
$elem = new ZendX_JQuery_Form_Element_AutoComplete(
'query',
array('Label' => 'Search',
'required'=>true,
'filters'=>array('StripTags'),
'validators'=>array(
array('validator'=>'StringLength',
'options'=>array('min'=>'3'),
'breakChainOnFailure'=>true
),
array('Alnum')
)
)
);
$elem->setJQueryParams(array('data' => array(),
'url' => 'my_autocomplete_callback.php',
'minChars' => 1,
'onChangeInterval' => 500,
)
);
然后,我什至更改了默认的装饰器,如下所示:
Then I even changed the default decorators like this:
$elementDecorators = array(
array('UiWidgetElement', array('tag' => '')),
array('Errors', array('tag' => 'div', 'class'=>'error')),
array('Label'),
array('HtmlTag', array('tag' => 'div')),
);
$elem->setDecorators($elementDecorators);
最后添加到我的表单中(请记住我在init()中,所以我将通过$ this来解决它):
And finally add to my form (remember I'm in the init() so I'll address it via $this):
$this->addElement($elem);
你在这里,魔术已经完成了.
There you are, the magic is done.
PS:别忘了在引导程序中添加以下内容:
PS: don't forget to add the following in your bootstrap:
$view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
这篇关于Zend Framework表单与jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!