在 Zend 中创建表单视图

发布于 2024-10-15 06:53:08 字数 1637 浏览 2 评论 0原文

有没有可靠的方法来为 Zend_Form 创建自定义视图?装饰器非常神秘,有时以奇特的方式使用它们非常复杂,以至于我宁愿手动编写 HTML。有没有办法做到这一点,并且仍然使表单与控制器完全配合(例如,调用 $form->isValid() 并期望一切都能正确验证)?如果是这样,是否有任何需要注意的警告(例如注意验证错误显示)?

理想的解决方案是创建一个表单并传递 elements 数组(包含必要的数据,如名称、ID、输入类型以及渲染 HTML 所需的所有数据)——Zend Framework 允许这样做吗?

[编辑]

我尝试在视图中添加此代码(一个简单的登录表单),而不是仅仅echoing表单对象:

<?php
$userid = $this->form->getElement('userid');
$pass = $this->form->getElement('password');
$remember = $this->form->getElement('remember');
$submit = $this->form->getElement('submit');
?>
<form enctype="<?php echo $this->form->getEnctype(); ?>" method="<?php echo $this->form->getMethod(); ?>" action="<?php echo $this->form->getAction(); ?>" id="<?php echo $this->form->getId(); ?>">
name: <input type="text" id="<?php echo $userid->getId(); ?>" name="<?php echo $userid->getName(); ?>" /><br />
pass: <input type="password" id="<?php echo $pass->getId(); ?>" name="<?php echo $pass->getName(); ?>" /><br />
remember: <input type="checkbox" id="<?php echo $remember->getId(); ?>" name="<?php echo $remember->getName(); ?>" /><br />
submit: <input type="submit" id="<?php echo $submit->getId(); ?>" name="<?php echo $submit->getName(); ?>" value="<?php echo $submit->getValue(); ?>" />
</form>

该表单似乎工作正常并验证(尽管我没有得到重定向了我来自的页面 - 但我相信这是一个不同的问题,因为我通过 GET 传递该页面,而不是以表单形式)。这是可以接受的,还是我在不知不觉中做了一些可怕的错误?

Is there a reliable way to create a custom view for a Zend_Form? The decorators are pretty cryptic and using them in fancy ways sometimes is so complicated that I'd prefer to just write the HTML by hand. Is there a way to do it and still make the form fully cooperate with the controller (eg. call $form->isValid() and expect everything to validate properly)? If so, are there any caveats to look out for (like taking care about validation errors display)?

The ideal solution would be to create a form and pass the elements array (containing the necessary data like names, IDs, input types and all needed to render the HTML) - does Zend Framework permit this?

[EDIT]

Instead of just echoing the form object, I have tried adding this code in the view (a simple login form):

<?php
$userid = $this->form->getElement('userid');
$pass = $this->form->getElement('password');
$remember = $this->form->getElement('remember');
$submit = $this->form->getElement('submit');
?>
<form enctype="<?php echo $this->form->getEnctype(); ?>" method="<?php echo $this->form->getMethod(); ?>" action="<?php echo $this->form->getAction(); ?>" id="<?php echo $this->form->getId(); ?>">
name: <input type="text" id="<?php echo $userid->getId(); ?>" name="<?php echo $userid->getName(); ?>" /><br />
pass: <input type="password" id="<?php echo $pass->getId(); ?>" name="<?php echo $pass->getName(); ?>" /><br />
remember: <input type="checkbox" id="<?php echo $remember->getId(); ?>" name="<?php echo $remember->getName(); ?>" /><br />
submit: <input type="submit" id="<?php echo $submit->getId(); ?>" name="<?php echo $submit->getName(); ?>" value="<?php echo $submit->getValue(); ?>" />
</form>

The form seems to work OK and validate (although I don't get redirected the the page I came from - but that's a different problem, I believe, as I pass that via GET, not in the form). Is that acceptable, or am I doing something horridly wrong without knowing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海螺姑娘 2024-10-22 06:53:08

很大程度上取决于设计和最终布局。

您想在哪里显示验证错误?如何?通过错误装饰器?
您还想使用描述吗?
需要使用其他装饰器吗?

最好的方法似乎是只创建您自己的 Zend_Form_Element_ThreadIcons 元素。这就像子类化 Zend_Form 元素之一并实现返回您需要的 HTML 的自定义 _render() 方法一样简单。您甚至可以在那里使用您自己的 View 实例。

然后,您可以将图标数组作为元素选项传递,并按照您需要的方式处理它。

如果您决定使用装饰器,您会发现此演示文稿对于掌握该技术非常有用:

A lot depends of the design and the final layout.

Where do you want to display validation errors? How? Via Error decorator?
Do you want also use descriptions?
Do you need to use other decorators?

The best way seems to be to create just your own Zend_Form_Element_ThreadIcons element. This is as easy as subclassing one of Zend_Form elements and implementing custom _render() method returning HTML you need. You may even use your own View instance there.

Then you may pass array of icons as an element option and handle it the way you need.

If you decide using decorators, you my find this presentation very useful to master the technique:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文