From: Andrey Kutejko Date: Sat, 10 Aug 2013 14:40:55 +0000 (+0300) Subject: simplify widget creation X-Git-Tag: 0.5~75 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=a2a560d4e5a126425a65ff50d60d5e97f46b1826;p=ipf.git simplify widget creation --- diff --git a/ipf/form/field.php b/ipf/form/field.php index 46dbcdf..bd87d67 100644 --- a/ipf/form/field.php +++ b/ipf/form/field.php @@ -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); } } diff --git a/ipf/form/field/html.php b/ipf/form/field/html.php index ed985e8..6c44320 100644 --- a/ipf/form/field/html.php +++ b/ipf/form/field/html.php @@ -1,7 +1,7 @@ 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); } }