]> git.andy128k.dev Git - ipf.git/commitdiff
simplify widget creation
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 10 Aug 2013 14:40:55 +0000 (17:40 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 10 Aug 2013 14:40:55 +0000 (17:40 +0300)
ipf/form/field.php
ipf/form/field/html.php
ipf/form/field/varchar.php

index 46dbcdf057bb10a1b13f0b8c7e4970ab6ab5dec7..bd87d679ca3037d43346945cafa490704c1b7d00 100644 (file)
@@ -28,18 +28,9 @@ class IPF_Form_Field
             if ($key !== 'widget_attrs')
                 $this->$key = $m[$key];
         }
-        $widget_name = $this->getWidget();
-        if (isset($params['widget_attrs'])) {
-            $attrs = $params['widget_attrs'];
-        } else {
-            $attrs = array();
-        }
-        $widget = new $widget_name($attrs);
-        $attrs = $this->widgetAttrs($widget);
-        if (count($attrs)) {
-            $widget->attrs = array_merge($widget->attrs, $attrs);
-        }
-        $this->widget = $widget;
+
+        $widget_attrs = isset($params['widget_attrs']) ? $params['widget_attrs'] : array();
+        $this->widget = $this->createWidget($widget_attrs);
     }
 
     public function clean($value)
@@ -53,13 +44,10 @@ class IPF_Form_Field
     function LateClean($data, &$cleaned_data){
     }
 
-    protected function getWidget(){
-        return $this->widget;
-    }
-
-    public function widgetAttrs($widget)
+    protected function createWidget($args)
     {
-        return array();
+        $widgetClass = $this->widget;
+        return new $widgetClass($args);
     }
 }
 
index ed985e807d804001b2a6d83a92e8f7722fe1274c..6c44320d86858e90a376e73c4e97c382c48b7c98 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 
-class IPF_Form_Field_Html extends IPF_Form_Field_Varchar{
-    protected function getWidget(){
-        return 'IPF_Form_Widget_HTMLInput';
-    }
-}
\ No newline at end of file
+class IPF_Form_Field_Html extends IPF_Form_Field_Varchar
+{
+    public $widget = 'IPF_Form_Widget_HTMLInput';
+}
+
index 6946d95f6c067a4d3fa698affa3d073828ff6316..804576a0a563a852106cd3aca22888f684fa9e75 100644 (file)
@@ -22,19 +22,16 @@ class IPF_Form_Field_Varchar extends IPF_Form_Field
         return $value;
     }
 
-    public function widgetAttrs($widget)
+    protected function createWidget($args)
     {
-        if ($this->max_length !== null and in_array(get_class($widget), array('IPF_Form_Widget_TextInput', 'IPF_Form_Widget_PasswordInput'))) {
-            return array('maxlength'=>$this->max_length);
+        if ($this->max_length > 255) {
+            $widgetClass = 'IPF_Form_Widget_TextareaInput';
+        } else {
+            $widgetClass = $this->widget;
+            if ($this->max_length !== null)
+                $args['maxlength'] = $this->max_length;
         }
-        return array();
-    }
-    
-    protected function getWidget()
-    {
-        if ($this->max_length>255)
-            return 'IPF_Form_Widget_TextareaInput';
-        return $this->widget;
+        return new $widgetClass($args);
     }
 }