It is a form's responsibility to process the request it was submitted with

How forms are being validated should not be a task that is part of the
controller action, but should be handled by the form base class as it is
most of the time the same procedure. Now a controller action just sets
up the form to use, calls handleRequest() and acts upon its return value.
(e.g. calling handleRequest() from another form or any redirection)

refs #5525
This commit is contained in:
Johannes Meyer 2014-08-25 17:06:45 +02:00
parent c93ab7951d
commit 52534a2f46

View File

@ -190,6 +190,30 @@ class Form extends Zend_Form
return array(); return array();
} }
/**
* Perform actions after this form was submitted using a valid request
*
* Intended to be implemented by concrete form classes.
*
* @param Request $request The valid request used to process this form
*/
public function onSuccess(Request $request)
{
}
/**
* Perform actions after this form was submitted using an invalid request
*
* Intended to be implemented by concrete form classes.
*
* @param Request $request The invalid request supposed to process this form
*/
public function onFailure(Request $request)
{
}
/** /**
* Add a submit button to this form * Add a submit button to this form
* *
@ -292,6 +316,37 @@ class Form extends Zend_Form
return parent::setDefaults($defaults); return parent::setDefaults($defaults);
} }
/**
* Process the given request using this form
*
* @param Request $request The request to be processed
*
* @return null|bool True in case the request was handled and valid,
* false if invalid and null if it was not handled
*/
public function handleRequest(Request $request)
{
if (strtolower($request->getMethod()) === $this->getMethod()) {
$formData = $request->{'get' . $request->isPost() ? 'Post' : 'Query'}();
if ($this->wasSent($formData)) {
$this->populate($formData); // Necessary to get isSubmitted() to work
if ($this->isSubmitted()) {
if ($this->isValid($formData)) {
$this->onSuccess($request);
return true;
} else {
$this->onFailure($request);
}
} else {
// The form can't be processed but we want to show validation errors though
$this->isValidPartial($formData);
}
return false;
}
}
}
/** /**
* Return whether the submit button of this form was pressed * Return whether the submit button of this form was pressed
* *