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;
}
}
精彩评论