【问题标题】:HTML form with Zend Form filters/validators带有 Zend Form 过滤器/验证器的 HTML 表单
【发布时间】:2012-04-01 06:43:52
【问题描述】:

我正在我的 ZF 应用程序中创建一个简单的联系表单。只为几个表单元素操作装饰器感觉不值得。

我的问题是:我是否仍然可以对使用 Zend Form 创建的元素使用 Zend Form Filters:

例如:

形式:

<!-- Standard HTML - not generated with ZF -->
<form id="contact-form" method="post" action="/contact/submit">
    <input type="text" name="name" />
    <input type="email" name="email" />
    <input type="submit" name="submit" />
</form>

控制器:

public function submitAction()
{
    $params = $this->_request->getParams();
    //Am I able to apply filters/validators to the data I get from the request?
    //What is the best way to handle this?
}

我查看了this(Darcy Hastings 的回答)——它似乎可以工作,只是感觉有点 hacky。

任何和所有建议都表示赞赏。

谢谢, 肯

【问题讨论】:

  • 我同意,我也不喜欢 Darcy Hastings 的回答。

标签: html forms zend-framework zend-form


【解决方案1】:

是的,您可以使用Zend_Filter_Input,这里有一个设置示例。

       //set filters and validators for Zend_Filter_Input
        $filters = array(
            'trackid' => array('HtmlEntities', 'StripTags')
        );

        $validators = array(
            'trackid' => array('NotEmpty', 'Int')
        );

        //assign Input
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());

            //check input is valid and is specifically posted as 'Delete Selected'
            if ($input->isValid()) {

你也可以考虑使用 viewscript 装饰器来渲染 Zend Form,控件是绝对的(或几乎)。:

//in your controller action
public function indexAction() {
        //a normally constructed Zend_Form
        $form = new Member_Form_Bid();
        $form->setAction('/member/bid/preference');
        //attach a partial to display the form, this is the decrator
        $form->setDecorators(array(
            array('ViewScript', array('viewScript' => '_bidForm.phtml'))
        ));
  $this->view->form = $form;

//the view
<?php echo $this->form?>

//the partial
//use a normal Zend_Form and display only the parts you want
//processing in an action is done like any other Zend_Form
form action="<?php echo $this->element->getAction() ?>"
      method="<?php echo $this->element->getMethod() ?>">
    <table id="sort">
        <tr>
            <th colspan="2">Sort By Shift</th>
            <th colspan="2">Sort By Days Off</th>
            <th colspan="2">Sort By Bid Location</th>
        </tr>
        <tr></tr>
        <tr>
            <td class="label"><?php echo $this->element->shift->renderLabel() ?></td>
            <td class="element"><?php echo $this->element->shift->renderViewHelper() ?></td>
            <td class="label"><?php echo $this->element->weekend->renderLabel() ?></td>
            <td class="element"><?php echo $this->element->weekend->renderViewHelper() ?></td>
            <td class="label"><?php echo $this->element->bidlocation->renderLabel() ?></td>
            <td class="element"><?php echo $this->element->bidlocation->renderViewHelper() ?></td>
        </tr>
        <tr></tr>
        <tr>
            <td colspan="6" style="text-align: center"><?php echo $this->element->submit ?></td>
        </tr>
    </table>   
</form>

【讨论】:

  • 这里有什么您需要与addPrefixPath 做的事情吗?收到此错误Plugin by name 'Email' was not found in the registry; used paths: Zend_Form_Element_: Zend/Form/Element/,根据我的搜索,这是我收集的唯一内容。
  • Nvm,这是因为我在 Zend/Form/Element 中没有 Email 类型的元素。
【解决方案2】:

是的,您绝对可以在自渲染表单上使用Zend_Form

您可以通过两种方式做到这一点:

  1. 使用Zend_Form 对象,但不要渲染它。你像往常一样创建一个Zend_Form 实例,所有元素都正确命名,并像往常一样附加验证器和过滤器。在您的操作中,您可以检查表单的isValid() 并使用getValues() 来确保您收集过滤后的数据。

  2. 第二个选项是使用Zend_Filter_Input,它是一个验证器和过滤器链。您在构建时设置验证器和过滤器,然后调用 setData 以使用请求中的信息填充过滤器。同样,您要测试 isValid(),然后使用 getUnescaped() 检索数据。 manual page 有更多详细信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多