diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 20484c33e..787e8dc27 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -11,7 +11,6 @@ use Zend_View_Interface; use Icinga\Application\Icinga; use Icinga\Web\Form\Decorator\NoScriptApply; use Icinga\Web\Form\Element\CsrfCounterMeasure; -use Icinga\Web\Form\FormElement; /** * Base class for forms providing CSRF protection, confirmation logic and auto submission @@ -37,13 +36,6 @@ class Form extends Zend_Form */ protected $created = false; - /** - * The request associated with this form - * - * @var Request - */ - protected $request; - /** * The callback to call instead of Form::onSuccess() * @@ -108,38 +100,31 @@ class Form extends Zend_Form * @var array */ public static $defaultElementDecorators = array( - array('ViewHelper', array('separator' => '')), - array('Errors', array('separator' => '')), - array('Description', array('tag' => 'span', 'class' => 'description', 'separator' => '')), - array('Label', array('separator' => '')), - array('HtmlTag', array('tag' => 'div', 'class' => 'element')) + 'ViewHelper', + 'Errors', + array('Description', array('tag' => 'span', 'class' => 'description')), + 'Label', + array('HtmlTag', array('tag' => 'div')) ); /** - * Create a new form + * Set a callback that is called instead of this form's onSuccess method * - * Accepts an additional option `onSuccess' which is a callback that is called instead of this - * form's method. It is called using the following signature: (Form $form). + * It is called using the following signature: (Request $request, Form $form). * - * @see Zend_Form::__construct() + * @param callable $onSuccess Callback * - * @throws LogicException In case `onSuccess' is not callable + * @return $this + * + * @throws LogicException If the callback is not callable */ - public function __construct($options = null) + public function setOnSuccess($onSuccess) { - if (is_array($options) && isset($options['onSuccess'])) { - $this->onSuccess = $options['onSuccess']; - unset($options['onSuccess']); - } elseif (isset($options->onSuccess)) { - $this->onSuccess = $options->onSuccess; - unset($options->onSuccess); - } - - if ($this->onSuccess !== null && false === is_callable($this->onSuccess)) { + if (! is_callable($onSuccess)) { throw new LogicException('The option `onSuccess\' is not callable'); } - - parent::__construct($options); + $this->onSuccess = $onSuccess; + return $this; } /** @@ -363,9 +348,11 @@ class Form extends Zend_Form * * Intended to be implemented by concrete form classes. The base implementation returns always FALSE. * + * @param Request $request The valid request used to process this form + * * @return null|bool Return FALSE in case no redirect should take place */ - public function onSuccess() + public function onSuccess(Request $request) { return false; } @@ -374,8 +361,10 @@ class Form extends Zend_Form * Perform actions when no form dependent data was sent * * Intended to be implemented by concrete form classes. + * + * @param Request $request The current request */ - public function onRequest() + public function onRequest(Request $request) { } @@ -441,8 +430,8 @@ class Form extends Zend_Form * `disableLoadDefaultDecorators' option to any other value than `true'. For loading custom element decorators use * the 'decorators' option. * - * @param string $type The type of the element - * @param string $name The name of the element + * @param string $type String element type + * @param string $name The name of the element to add * @param mixed $options The options for the element * * @return Zend_Form_Element @@ -464,20 +453,10 @@ class Form extends Zend_Form $options = array('decorators' => static::$defaultElementDecorators); } - if (($el = $this->createIcingaFormElement($type, $name, $options)) === null) { - $el = parent::createElement($type, $name, $options); - } + $el = parent::createElement($type, $name, $options); if ($el && $el->getAttrib('autosubmit')) { - $noScript = new NoScriptApply(); // Non-JS environments - $decorators = $el->getDecorators(); - $pos = array_search('Zend_Form_Decorator_ViewHelper', array_keys($decorators)) + 1; - $el->setDecorators( - array_slice($decorators, 0, $pos, true) - + array(get_class($noScript) => $noScript) - + array_slice($decorators, $pos, count($decorators) - $pos, true) - ); - + $el->addDecorator(new NoScriptApply()); // Non-JS environments $class = $el->getAttrib('class'); if (is_array($class)) { $class[] = 'autosubmit'; @@ -487,7 +466,6 @@ class Form extends Zend_Form $class .= ' autosubmit'; } $el->setAttrib('class', $class); // JS environments - unset($el->autosubmit); } @@ -555,17 +533,15 @@ class Form extends Zend_Form { if ($request === null) { $request = $this->getRequest(); - } else { - $this->request = $request; } - $formData = $this->getRequestData(); + $formData = $this->getRequestData($request); if ($this->getUidDisabled() || $this->wasSent($formData)) { $this->populate($formData); // Necessary to get isSubmitted() to work if (! $this->getSubmitLabel() || $this->isSubmitted()) { if ($this->isValid($formData) - && (($this->onSuccess !== null && false !== call_user_func($this->onSuccess, $this)) - || ($this->onSuccess === null && false !== $this->onSuccess()))) { + && (($this->onSuccess !== null && false !== call_user_func($this->onSuccess, $request, $this)) + || ($this->onSuccess === null && false !== $this->onSuccess($request)))) { $this->getResponse()->redirectAndExit($this->getRedirectUrl()); } } else { @@ -573,7 +549,7 @@ class Form extends Zend_Form $this->isValidPartial($formData); } } else { - $this->onRequest(); + $this->onRequest($request); } return $request; @@ -699,19 +675,29 @@ class Form extends Zend_Form } /** - * Return the request associated with this form + * Return the request data based on this form's request method * - * Returns the global request if none has been set for this form yet. + * @param Request $request The request to fetch the data from + * + * @return array + */ + public function getRequestData(Request $request) + { + if (strtolower($request->getMethod()) === $this->getMethod()) { + return $request->{'get' . ($request->isPost() ? 'Post' : 'Query')}(); + } + + return array(); + } + + /** + * Return the current request * * @return Request */ public function getRequest() { - if ($this->request === null) { - $this->request = Icinga::app()->getFrontController()->getRequest(); - } - - return $this->request; + return Icinga::app()->getFrontController()->getRequest(); } /** @@ -724,39 +710,6 @@ class Form extends Zend_Form return Icinga::app()->getFrontController()->getResponse(); } - /** - * Return the request data based on this form's request method - * - * @return array - */ - protected function getRequestData() - { - if (strtolower($this->request->getMethod()) === $this->getMethod()) { - return $this->request->{'get' . ($this->request->isPost() ? 'Post' : 'Query')}(); - } - - return array(); - } - - /** - * Create a new element located in the Icinga Web 2 library - * - * @param string $type The type of the element - * @param string $name The name of the element - * @param mixed $options The options for the element - * - * @return NULL|FormElement NULL in case the element is not found in the Icinga Web 2 library - * - * @see Form::$defaultElementDecorators For Icinga Web 2's default element decorators. - */ - protected function createIcingaFormElement($type, $name, $options = null) - { - $className = 'Icinga\\Web\\Form\\Element\\' . ucfirst($type); - if (class_exists($className)) { - return new $className($name, $options); - } - } - /** * Render this form * @@ -770,3 +723,4 @@ class Form extends Zend_Form return parent::render($view); } } +