Model view controller 哪里是放置表单验证的最佳位置

Model view controller 哪里是放置表单验证的最佳位置,model-view-controller,zend-framework,Model View Controller,Zend Framework,好的,我来自Flex/ActionScript的背景,因为这个领域正在慢慢消亡,我想是时候转向全职PHP了。 无论如何,我认为我对Zend框架有很好的理解(我工作过的大多数地方都使用它)。然而,他们使用它主要是为了表抽象类,所以它实际上不是一个好的MVC实践,更像是一个模型。 参加面试时,我几乎总是被问到表单验证在哪里进行。 现在我知道zend已经内置了表单验证程序,我不是在说这些。 我说的是在视图中构建并提交到服务器的标准HTML表单。 我想知道的问题是,是在控件中还是在模型中进行更好。

好的,我来自Flex/ActionScript的背景,因为这个领域正在慢慢消亡,我想是时候转向全职PHP了。
无论如何,我认为我对Zend框架有很好的理解(我工作过的大多数地方都使用它)。然而,他们使用它主要是为了表抽象类,所以它实际上不是一个好的MVC实践,更像是一个模型。
参加面试时,我几乎总是被问到表单验证在哪里进行。
现在我知道zend已经内置了表单验证程序,我不是在说这些。
我说的是在视图中构建并提交到服务器的标准HTML表单。

我想知道的问题是,是在控件中还是在模型中进行更好。


还有,为什么要使用zend_表单呢?对我来说,设计师似乎很难让它变得性感。

如果将验证器附加到表单元素,表单将在控制器中验证,并可以使用简单的If()循环进行检查:

否则,您可以使用以下方法验证控制器中的简单html表单或其他输入:

无论哪种方式,您都希望在控制器中至少执行基本验证和筛选,以尝试防止跨站点脚本编写和sql注入。
如果需要,您始终可以在域模型中进行更深入的验证和筛选。

至于为什么要使用Zend_表单,它并不像最初看起来的那么糟糕。将验证器和筛选器附加到自定义消息的功能对某些人非常有用。装修师虽然一开始很难,但可以在实践中得到很好的运用。你也会作弊,只是用这个(就像我通常做的那样)。viewScript装饰器以更熟悉的形式提供了大量控件。

以下是一个例子:

//The Form
class Application_Form_Search extends Zend_Form
{
    public function init() {
        $this->setMethod('POST');
        $this->setDecorators(array(
            array('ViewScript', array(
                    'viewScript' => '_searchForm.phtml'//the partial script used as a decorator
            ))
        ));
        // create new element
        $query = $this->createElement('text', 'query');
        // element options
        $query->setLabel('Search Keywords');
        $query->setAttribs(array('placeholder' => 'Artist or Title',
            'size' => 27,
        ));
        // add the element to the form
        $this->addElement($query);

        $submit = $this->createElement('submit', 'search');
        $submit->setLabel('Search Site');
        $submit->setDecorators(array('ViewHelper'));
        $this->addElement($submit);
    }
}
部分脚本:

<article class="search">
    <form action="<?php echo $this->element->getAction() ?>"
          method="<?php echo $this->element->getMethod() ?>">
        <table>
            <tr>
                <th><?php echo $this->element->query->renderLabel() ?></th>
            </tr>
            <tr>
                <td><?php echo $this->element->query->renderViewHelper() ?></td>
            </tr>
            <tr>
                <td><?php echo $this->element->search ?></td>
            </tr>
        </table> 
    </form>
</article>


验证是模型的一部分,因为它知道验证某个实体的业务逻辑,但在控制器内部使用

Zend_表单附带了类似于decorator的ViewScript,它使desinger变得性感,并使开发人员能够轻松完成验证、过滤和填充表单输入的工作

//The Form
class Application_Form_Search extends Zend_Form
{
    public function init() {
        $this->setMethod('POST');
        $this->setDecorators(array(
            array('ViewScript', array(
                    'viewScript' => '_searchForm.phtml'//the partial script used as a decorator
            ))
        ));
        // create new element
        $query = $this->createElement('text', 'query');
        // element options
        $query->setLabel('Search Keywords');
        $query->setAttribs(array('placeholder' => 'Artist or Title',
            'size' => 27,
        ));
        // add the element to the form
        $this->addElement($query);

        $submit = $this->createElement('submit', 'search');
        $submit->setLabel('Search Site');
        $submit->setDecorators(array('ViewHelper'));
        $this->addElement($submit);
    }
}
<article class="search">
    <form action="<?php echo $this->element->getAction() ?>"
          method="<?php echo $this->element->getMethod() ?>">
        <table>
            <tr>
                <th><?php echo $this->element->query->renderLabel() ?></th>
            </tr>
            <tr>
                <td><?php echo $this->element->query->renderViewHelper() ?></td>
            </tr>
            <tr>
                <td><?php echo $this->element->search ?></td>
            </tr>
        </table> 
    </form>
</article>