开发者

stop further execution after zend form is not valid and populate the form

开发者 https://www.devze.com 2023-03-05 10:36 出处:网络
if the f开发者_高级运维orm is not valid then populate the form and stop further execution of code. How to establish this.Here\'s what I usually do in my controller:

if the f开发者_高级运维orm is not valid then populate the form and stop further execution of code. How to establish this.


Here's what I usually do in my controller:

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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消