Implemented error handling within FormDocument
authorAlexander Ebert <ebert@woltlab.com>
Wed, 22 Feb 2012 22:05:55 +0000 (23:05 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 22 Feb 2012 22:05:55 +0000 (23:05 +0100)
Fixes #436

wcfsetup/install/files/lib/system/form/FormDocument.class.php
wcfsetup/install/files/lib/system/form/IFormElement.class.php
wcfsetup/install/files/lib/system/form/IFormElementContainer.class.php
wcfsetup/install/files/lib/system/form/container/AbstractFormElementContainer.class.php
wcfsetup/install/files/lib/system/form/element/AbstractFormElement.class.php
wcfsetup/install/files/lib/system/form/element/LabelFormElement.class.php
wcfsetup/install/files/lib/system/form/element/PasswordInputFormElement.class.php
wcfsetup/install/files/lib/system/form/element/TextInputFormElement.class.php
wcfsetup/install/files/lib/system/package/PackageInstallationDispatcher.class.php
wcfsetup/install/lang/de.xml
wcfsetup/install/lang/en.xml

index 160365941ebac2ca3a0f8d4a099d9675df327120..7976f3d2f18ffc48a2c3ebfbe5ab0019bc9ce54a 100644 (file)
@@ -121,4 +121,17 @@ class FormDocument {
                        }
                }
        }
+       
+       /**
+        * Sets localized error message for given element.
+        * 
+        * @param       string          $name
+        * @param       string          $error
+        * @param unknown_type $error
+        */
+       public function setError($name, $error) {
+               foreach ($this->container as $container) {
+                       $this->containers->setError($name, $error);
+               }
+       }
 }
index 3f824892bcfdcc9dc4fa12a028fcfe8d19fcead1..295abcc61c6ea559a1a95818740d508cc810357a 100644 (file)
@@ -61,4 +61,18 @@ interface IFormElement {
         * @return      string
         */
        public function getHTML($formName);
+       
+       /**
+        * Sets localized error message.
+        * 
+        * @param       string          $error
+        */
+       public function setError($error);
+       
+       /**
+        * Returns localized error message, empty if no error occured.
+        * 
+        * @return      string
+        */
+       public function getError();
 }
index e52a1bce0092b38e9db0f636dc70ea35fa89274e..9c4dfe414e67b07ad6cabd2d0aa72360bd3cc3f8 100644 (file)
@@ -83,4 +83,12 @@ interface IFormElementContainer {
         * @return      string
         */
        public function getHTML($formName);
+       
+       /**
+        * Sets localized error message for named element.
+        * 
+        * @param       string          $name
+        * @param       string          $error
+        */
+       public function setError($name, $error);
 }
index afaf92a45efbab137f156823361ef5d754221eb9..a8115d71d6f03266852755ce62a9cfd304198ce7 100644 (file)
@@ -104,7 +104,7 @@ abstract class AbstractFormElementContainer implements IFormElementContainer {
        public function handleRequest(array $variables) {
                foreach ($this->children as $element) {
                        if (!($element instanceof AbstractNamedFormElement)) {
-                               return;
+                               continue;
                        }
                        
                        if (isset($variables[$element->getName()])) {
@@ -112,4 +112,19 @@ abstract class AbstractFormElementContainer implements IFormElementContainer {
                        }
                }
        }
+       
+       /**
+        * @see wcf\system\form\IFormElementContainer::setError()
+        */
+       public function setError($name, $error) {
+               foreach ($this->children as $element) {
+                       if (!($element instanceof AbstractNamedFormElement)) {
+                               continue;
+                       }
+                       
+                       if ($element->getName() == $name) {
+                               $element->setError($error);
+                       }
+               }
+       }
 }
index 7aa006614739c68e093dd425d776a95e97b70056..766dfd6dffcd54e8a71fb1f30d572654772f9274 100644 (file)
@@ -21,6 +21,12 @@ abstract class AbstractFormElement implements IFormElement {
         */
        protected $description = '';
        
+       /**
+        * localized error string
+        * @var string
+        */
+       protected $error = '';
+       
        /**
         * element label
         * @var string
@@ -74,4 +80,36 @@ abstract class AbstractFormElement implements IFormElement {
        public function getParent() {
                return $this->parent;
        }
+       
+       /**
+        * @see wcf\system\form\IFormElement::setError()
+        */
+       public function setError($error) {
+               $this->error = $error;
+       }
+       
+       /**
+        * @see wcf\system\form\IFormElement::getError()
+        */
+       public function getError() {
+               return $this->error;
+       }
+       
+       /**
+        * Returns class attribute if an error occured.
+        * 
+        * @return      string
+        */
+       protected function getErrorClass() {
+               return ($this->getError()) ? ' class="wcf-formError"' : '';
+       }
+       
+       /**
+        * Returns an error message if occured.
+        * 
+        * @return      string
+        */
+       protected function getErrorField() {
+               return ($this->getError()) ? '<small class="wcf-innerError">'.$this->getError().'</small>' : '';
+       }
 }
index 00f86bcd70ae7795722a9d4f6ee259f1548b8a94..2e8046c5599a005241eb9a1e7f6edfd9143097cf 100644 (file)
@@ -42,11 +42,12 @@ class LabelFormElement extends AbstractFormElement {
         */
        public function getHTML($formName) {
                return <<<HTML
-<dl>
+<dl{$this->getErrorClass()}>
        <dt><label>{$this->getLabel()}</label></dt>
        <dd>
                {$this->getText()}
                <small>{$this->getDescription()}</small>
+               {$this->getErrorField()}
        </dd>
 </dl>
 HTML;
index 4c6ba3e3396334564dfad14c787df18b90eb64ad..8ffaa8d38895393c493f10b47712c3a56e68cfd1 100644 (file)
@@ -18,11 +18,12 @@ class PasswordInputFormElement extends AbstractNamedFormElement {
         */
        public function getHTML($formName) {
                return <<<HTML
-<dl>
+<dl{$this->getErrorClass()}>
        <dt><label for="{$this->getName()}">{$this->getLabel()}</label></dt>
        <dd>
                <input type="password" id="{$this->getName()}" name="{$formName}{$this->getName()}" value="{$this->getValue()}" class="medium" />
                <small>{$this->getDescription()}</small>
+               {$this->getErrorField()}
        </dd>
 </dl>
 HTML;
index 20d32088a03867b6700f0c26508087b264100d88..9f9441d760118cdf1a98eeef2e14058fdafa9f9c 100644 (file)
@@ -18,11 +18,12 @@ class TextInputFormElement extends AbstractNamedFormElement {
         */
        public function getHTML($formName) {
                return <<<HTML
-<dl>
+<dl{$this->getErrorClass()}>
        <dt><label for="{$this->getName()}">{$this->getLabel()}</label></dt>
        <dd>
                <input type="text" id="{$this->getName()}" name="{$formName}{$this->getName()}" value="{$this->getValue()}" class="long" />
                <small>{$this->getDescription()}</small>
+               {$this->getErrorField()}
        </dd>
 </dl>
 HTML;
index 6a84fa7aeb68239b75c448fa63c3bcd2decdef29..0fb67211badc3b0a804bf6124a5129a505b23cd8 100644 (file)
@@ -450,6 +450,13 @@ class PackageInstallationDispatcher {
                        $packageDir = $document->getValue('packageDir');
                        
                        if ($packageDir !== null) {
+                               // validate package dir
+                               if (file_exists(FileUtil::addLeadingSlash($packageDir) . 'global.php')) {
+                                       $document->setError('packageDir', WCF::getLanguage()->get('wcf.acp.package.packageDir.notAvailable'));
+                                       return null;
+                               }
+                               
+                               // set package dir
                                $packageEditor = new PackageEditor($this->getPackage());
                                $packageEditor->update(array(
                                        'packageDir' => FileUtil::getRelativePath(WCF_DIR, $packageDir)
index 88c16baeed6e56a1fd37e3b50ad3ccc61a20dfcb..9949a0bb4df2a73c157a5bcf6e90ef145b287c5a 100644 (file)
                <item name="wcf.acp.package.list.detailed"><![CDATA[Detaillierte Paketliste]]></item>
                <item name="wcf.acp.package.name"><![CDATA[Paket]]></item>
                <item name="wcf.acp.package.packageDate"><![CDATA[Erstellungsdatum]]></item>
+               <item name="wcf.acp.package.packageDir.notAvailable"><![CDATA[Das angegebene Verzeichnis enthält bereits eine Anwendung.]]></item>
                <item name="wcf.acp.package.plugin.installed"><![CDATA[Installierte Pakete]]></item>
                <item name="wcf.acp.package.plugin.title"><![CDATA[Erweiterungen]]></item>
                <item name="wcf.acp.package.source"><![CDATA[Datenquelle]]></item>
index 9e2199cdf17527ad25eba4338cb8688fa5fa2461..f15b345af6585127127028002819aaa5ac314b0e 100644 (file)
                <item name="wcf.acp.package.list.detailed"><![CDATA[Detailed package list]]></item>
                <item name="wcf.acp.package.name"><![CDATA[Package]]></item>
                <item name="wcf.acp.package.packageDate"><![CDATA[Creation date]]></item>
+               <item name="wcf.acp.package.packageDir.notAvailable"><![CDATA[The given directory already contains an application.]]></item>
                <item name="wcf.acp.package.plugin.installed"><![CDATA[Installed packages]]></item>
                <item name="wcf.acp.package.plugin.title"><![CDATA[Extensions]]></item>
                <item name="wcf.acp.package.source"><![CDATA[Data source]]></item>