在zend表单无效后停止进一步执行并填充表单

时间:2011-05-13 11:25:58

标签: zend-form

如果表单无效,则填充表单并停止进一步执行代码。如何建立这个。

1 个答案:

答案 0 :(得分:0)

这是我在控制器中通常做的事情:

public function editAction()
{
    $model = $this->_findModelFromRequestParams();

    $form = new Form_MyModelForm();
    $form->populate($model->toArray());

    //display the form for the first time and return
    if ( ! $this->_request->isPost()) {
        $this->view->form = $form;
        return;
    }

    //populate the form with the POST values, and validate. 
    //If NOT valid, display the form again
    if ( ! $form->isValid($this->_request->getPost())) {
        $this->view->form = $form;
        return;
    }

    try {
        $formData = $form->getValues();

        //save the new values here...

        //set a flash message and redirect to the view page

        $this->_redirect('/model/view/id/' . $model->id);

    } catch (Exception $e) {
        //there was some sort of error, so set a flash message with the error
        //and display the form again. $form is already populated with values 
        //since we called $form->isValid(), which populated the form
        $this->view->form = $form;
    }
}