Adds support for min/max value for integer option type
authorMatthias Schmidt <gravatronics@live.com>
Tue, 25 Dec 2012 15:53:46 +0000 (16:53 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Tue, 25 Dec 2012 15:53:46 +0000 (16:53 +0100)
The validation of the min/max value happens in the PHP class but can also be directly done in the browser because of the min and max properties of the input element.

com.woltlab.wcf/option.xml
com.woltlab.wcf/template/integerOptionType.tpl [new file with mode: 0644]
wcfsetup/install/files/acp/templates/integerOptionType.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/data/option/Option.class.php
wcfsetup/install/files/lib/system/option/IntegerOptionType.class.php

index 90ba1783aa193247edc6fc058d1d2bd771e28977..6038d55e88d73e5502487d3e2121a5f7b9c02f91 100644 (file)
                                <optiontype>boolean</optiontype>
                                <defaultvalue>0</defaultvalue>
                        </option>
-
+                       
                        <option name="enable_debug_mode">
                                <categoryname>module.system</categoryname>
                                <optiontype>boolean</optiontype>
@@ -242,6 +242,8 @@ imagick:wcf.acp.option.image_adapter_type.imagick]]>
                                <categoryname>general.system.http</categoryname>
                                <optiontype>integer</optiontype>
                                <defaultvalue>1</defaultvalue>
+                               <minvalue>1</minvalue>
+                               <maxvalue>9</maxvalue>
                        </option>
                        <!-- /general.system.http -->
                        
@@ -257,12 +259,13 @@ imagick:wcf.acp.option.image_adapter_type.imagick]]>
                                <categoryname>security.general.session</categoryname>
                                <optiontype>integer</optiontype>
                                <defaultvalue>1800</defaultvalue>
-                               <validationpattern>^[^0]</validationpattern>
+                               <minvalue>1</minvalue>
                        </option>
                        <option name="user_online_timeout">
                                <categoryname>security.general.session</categoryname>
                                <optiontype>integer</optiontype>
                                <defaultvalue>900</defaultvalue>
+                               <minvalue>1</minvalue>
                        </option>
                        <option name="session_validate_ip_address">
                                <categoryname>security.general.session</categoryname>
@@ -346,7 +349,7 @@ debug:mail_debug_logfile_path,!mail_use_f_param,!mail_smtp_host,!mail_smtp_port,
                        <option name="mail_smtp_port">
                                <categoryname>general.mail.send</categoryname>
                                <optiontype>integer</optiontype>
-                               <defaultvalue><![CDATA[25]]></defaultvalue>
+                               <defaultvalue>25</defaultvalue>
                        </option>
                        <option name="mail_smtp_user">
                                <categoryname>general.mail.send</categoryname>
diff --git a/com.woltlab.wcf/template/integerOptionType.tpl b/com.woltlab.wcf/template/integerOptionType.tpl
new file mode 100644 (file)
index 0000000..466d77b
--- /dev/null
@@ -0,0 +1 @@
+<input type="number" id="{$option->optionName}" name="values[{$option->optionName}]" value="{$value}"{if $option->minvalue !== null} min="{$option->minvalue}"{/if}{if $option->maxvalue !== null} max="{$option->maxvalue}"{/if}{if $inputClass} class="{@$inputClass}"{/if} />
\ No newline at end of file
diff --git a/wcfsetup/install/files/acp/templates/integerOptionType.tpl b/wcfsetup/install/files/acp/templates/integerOptionType.tpl
new file mode 100644 (file)
index 0000000..466d77b
--- /dev/null
@@ -0,0 +1 @@
+<input type="number" id="{$option->optionName}" name="values[{$option->optionName}]" value="{$value}"{if $option->minvalue !== null} min="{$option->minvalue}"{/if}{if $option->maxvalue !== null} max="{$option->maxvalue}"{/if}{if $inputClass} class="{@$inputClass}"{/if} />
\ No newline at end of file
index 09ab90dfe23c07da55a62ede35b8476baac2caef..7df9afa9e14a643e1b8b65f8fd804bffba162fa3 100644 (file)
@@ -26,6 +26,22 @@ class Option extends DatabaseObject {
         */
        protected static $databaseTableIndexName = 'optionID';
        
+       /**
+        * @see wcf\data\IStorableObject::__get()
+        */
+       public function __get($name) {
+               $value = parent::__get($name);
+               
+               // treat additional data as data variables if it is an array
+               if ($value === null) {
+                       if (is_array($this->data['additionalData']) && isset($this->data['additionalData'][$name])) {
+                               $value = $this->data['additionalData'][$name];
+                       }
+               }
+               
+               return $value;
+       }
+       
        /**
         * @see wcf\data\DatabaseObject::handleData()
         */
index 8250c2f23e19e52c9c520023b6e754e1eecf7138..f9bd62266a45cfd5bb54fefa2212d349a90469e9 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 namespace wcf\system\option;
 use wcf\data\option\Option;
+use wcf\system\exception\UserInputException;
+use wcf\system\WCF;
 
 /**
  * Option type implementation for integer input fields.
@@ -14,9 +16,22 @@ use wcf\data\option\Option;
  */
 class IntegerOptionType extends TextOptionType {
        /**
-        * @see wcf\system\option\TextOptionType::$inputType
+        * @see wcf\system\option\TextOptionType::$inputClass
         */
-       protected $inputType = 'number';
+       protected $inputClass = 'medium';
+       
+       /**
+        * @see wcf\system\option\IOptionType::getFormElement()
+        */
+       public function getFormElement(Option $option, $value) {
+               WCF::getTPL()->assign(array(
+                       'option' => $option,
+                       'inputClass' => $this->inputClass,
+                       'value' => $value
+               ));
+               
+               return WCF::getTPL()->fetch('integerOptionType');
+       }
        
        /**
         * @see wcf\system\option\IOptionType::getData()
@@ -24,4 +39,16 @@ class IntegerOptionType extends TextOptionType {
        public function getData(Option $option, $newValue) {
                return intval($newValue);
        }
+       
+       /**
+        * @see wcf\system\option\IOptionType::validate()
+        */
+       public function validate(Option $option, $newValue) {
+               if ($option->minvalue !== null && $option->minvalue > $newValue) {
+                       throw new UserInputException($option->optionName, 'tooLow');
+               }
+               if ($option->maxvalue !== null && $option->maxvalue < $newValue) {
+                       throw new UserInputException($option->optionName, 'tooHigh');
+               }
+       }
 }