From 52534a2f460aa760b539e63ebefa7994b94f718c Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 25 Aug 2014 17:06:45 +0200 Subject: [PATCH] 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 --- library/Icinga/Web/Form.php | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 8d2e4f333..54d89e271 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -190,6 +190,30 @@ class Form extends Zend_Form 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 * @@ -292,6 +316,37 @@ class Form extends Zend_Form 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 *