[icinga-checkins] icinga.org: icingaweb2/master: Introduce Form::setOnSuccess() in favor of overriding the constructor

git at icinga.org git at icinga.org
Wed Nov 19 12:15:40 CET 2014


Module: icingaweb2
Branch: master
Commit: 47414f352839b2cfcd34042fbdb0e885da692694
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=47414f352839b2cfcd34042fbdb0e885da692694

Author: Eric Lippmann <eric.lippmann at netways.de>
Date:   Tue Nov  4 16:15:06 2014 +0100

Introduce Form::setOnSuccess() in favor of overriding the constructor

Zend_Form uses setters for options if a respective setter method exists.
It is not necessary to override the constructor for introducing new options.

---

 library/Icinga/Web/Form.php |  136 ++++++++++++++-----------------------------
 1 file changed, 45 insertions(+), 91 deletions(-)

diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php
index 20484c3..787e8dc 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
@@ -38,13 +37,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()
      *
      * @var Callback
@@ -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
+     *
+     * It is called using the following signature: (Request $request, Form $form).
      *
-     * 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).
+     * @param   callable    $onSuccess  Callback
      *
-     * @see Zend_Form::__construct()
+     * @return  $this
      *
-     * @throws  LogicException      In case `onSuccess' is not callable
+     * @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,62 +675,39 @@ 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  Request
+     * @return  array
      */
-    public function getRequest()
+    public function getRequestData(Request $request)
     {
-        if ($this->request === null) {
-            $this->request = Icinga::app()->getFrontController()->getRequest();
+        if (strtolower($request->getMethod()) === $this->getMethod()) {
+            return $request->{'get' . ($request->isPost() ? 'Post' : 'Query')}();
         }
 
-        return $this->request;
-    }
-
-    /**
-     * Return the current Response
-     *
-     * @return  Response
-     */
-    public function getResponse()
-    {
-        return Icinga::app()->getFrontController()->getResponse();
+        return array();
     }
 
     /**
-     * Return the request data based on this form's request method
+     * Return the current request
      *
-     * @return  array
+     * @return  Request
      */
-    protected function getRequestData()
+    public function getRequest()
     {
-        if (strtolower($this->request->getMethod()) === $this->getMethod()) {
-            return $this->request->{'get' . ($this->request->isPost() ? 'Post' : 'Query')}();
-        }
-
-        return array();
+        return Icinga::app()->getFrontController()->getRequest();
     }
 
     /**
-     * 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
+     * Return the current Response
      *
-     * @see     Form::$defaultElementDecorators For Icinga Web 2's default element decorators.
+     * @return  Response
      */
-    protected function createIcingaFormElement($type, $name, $options = null)
+    public function getResponse()
     {
-        $className = 'Icinga\\Web\\Form\\Element\\' . ucfirst($type);
-        if (class_exists($className)) {
-            return new $className($name, $options);
-        }
+        return Icinga::app()->getFrontController()->getResponse();
     }
 
     /**
@@ -770,3 +723,4 @@ class Form extends Zend_Form
         return parent::render($view);
     }
 }
+



More information about the icinga-checkins mailing list