问题描述
我已经多次阅读手册,我已经搜索了 Google 提供的关于该主题的帖子,我什至还买了几本关于 ZF 的书.现在,为什么我仍然感到困惑?
I've read the manual many times, I've scoured the posts offered by Google on the subject, I have even bought a couple of books that deal with ZF. Now, why am I still confused?
我可以使用 Zend_Form 制作一个验证和功能正常的表单.我不能做的事情使表单看起来与我希望它看起来完全一样,并且带有我希望它具有的错误消息.我想要自定义按钮,我想要时髦的布局,我想要在表单中间插入文本等等.
I can, using Zend_Form, make a form that validates and functions fine. What I cannot do it make a form that looks exactly like I want it to look with the error messages that I want it to have. I want custom buttons, I want funky layouts, I want to insert text in the midst of the form, etc.
有没有人有实现这些事情的简单方法?有什么让我觉得这个框架是在为我节省时间而不是成本?我可以放弃 Zend Form...制作我自己的表单,让它的动作点击一个页面来验证和处理发布的数据,我可以尽可能快地输入,但我真的想得到"这个并且能够使用它,因为它显然是有意的.
Does anyone have a simple way of achieving these sorts of things? Something that makes me feel like the framework is saving me time rather than costing? I could forego Zend Form... make my own form, have its action hit a page to validate and process the posted data and I could do it about as fast as I can type but I really want to "get" this and be able to use it as it was apparently intended.
有什么建议吗?任何关于自定义按钮、时髦布局和基本(或相当高级,因为有大量基本教程跳过更难的问题)完成工作"的 zend 表单的任何简单操作方法"?
Any advice? Any simple "how to's" for custom buttons, funky layouts and basic (or rather advanced as there are tons of basic tutorials that skip over the harder issues) "getting things done" with zend form?
推荐答案
Barrett Conrad 的建议正是我所建议的.另外,请记住,您不需要使用表单对象来呈现表单.
Barrett Conrad's advice is what I would have suggested. Also, keep in mind that you don't need to use a form object to render your form.
您可以做的一件事是在视图脚本中创建一个表单,该表单具有与表单类同名的元素.
One thing you could do is create a form in your view script that has elements with the same name as a form class.
您的 HTML 表单:
Your HTML form:
<form action="/login/" method="post">
<fieldset>
<label for="username">Username:</label>
<input type="text" size="10" name="username" />
<label for="password">Password:</label>
<input type="password" size="10" name="password" />
<input type="submit" />
</fieldset>
</form>
你的班级:
class LoginForm extends Zend_Form
{
public function init()
{
$username = $this->createElement('text','username');
$username->setRequired(true);
$this->addElement($username);
$password = $this->createElement('password','password');
$password->setRequired(true);
$this->addElement($password);
}
}
您的表单类反映了您的 HTML 表单,类中的每个元素都有自己的验证器和要求.回到您的操作中,您可以创建表单类的实例并针对该表单验证您的 post/get 变量:
Your form class reflects your HTML form, each element in your class has its own validators and requirements. Back in your action you can create an instance of your form class and validate your post/get vars against that form:
$form = new LoginForm();
if ($this->_request->isPost()) {
if ($form->isValid($this->_request->getParams())) {
// do whatever you need to do
} else {
$this->view->errors = $form->getMessages();
}
}
您可以使用此方法在表单顶部显示一组错误消息.
You can display the the error messages at the top of your form in one group, using this method.
这是一个基本示例,但它允许您完全控制表单的呈现,而无需花时间学习使用装饰器.在我看来,Zend_Form 的大部分优势在于其验证和过滤属性.这给了你那种力量.这种解决方案的主要缺点是您的视图脚本 HTML 表单可能与您的表单类不同步.
This is a basic example, but it allows you to have total control over the presentation of your form without spending the time to learn to use decorators. Much of the strength of Zend_Form is in its validation and filtering properties, in my opinion. This gives you that strength. The main draw back to a solution like this is that your view script HTML form can become out-of-sync with your form class.
这篇关于Zend Form:我如何让它屈服于我的意志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!