From: Andrey Kutejko Date: Wed, 3 Sep 2014 18:46:54 +0000 (+0300) Subject: cleanup field and widget base classes X-Git-Tag: 0.6~147 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=c6ec7808b2911d8f14440919349cb60e4d02e681;p=ipf.git cleanup field and widget base classes --- diff --git a/ipf/form/field.php b/ipf/form/field.php index 741c1ed..c2bb3f5 100644 --- a/ipf/form/field.php +++ b/ipf/form/field.php @@ -6,8 +6,6 @@ class IPF_Form_Field public $label = ''; public $required = false; public $help_text = ''; - public $choices = null; - public $value = ''; /**< Current value of the field. */ protected $empty_values = array('', null, array()); function __construct($params=array()) diff --git a/ipf/form/field/choice.php b/ipf/form/field/choice.php index 75a1490..368caf0 100644 --- a/ipf/form/field/choice.php +++ b/ipf/form/field/choice.php @@ -3,18 +3,7 @@ class IPF_Form_Field_Choice extends IPF_Form_Field { public $widget = 'IPF_Form_Widget_SelectInput'; - protected $_choices = array(); - - function __construct($params=array()) - { - $this->_choices = \PFF\Arr::pop($params, 'choices', array()); - - $widget_attrs = \PFF\Arr::pop($params, 'widget_attrs', array()); - $widget_attrs['choices'] = $this->_choices; - $params['widget_attrs'] = $widget_attrs; - - parent::__construct($params); - } + public $choices = array(); public function clean($value) { @@ -29,10 +18,16 @@ class IPF_Form_Field_Choice extends IPF_Form_Field public function validValue($value) { - foreach ($this->_choices as $name => $val) + foreach ($this->choices as $name => $val) if ($value == $val) return true; return false; } + + protected function createWidget($args) + { + $args['choices'] = $this->choices; + return parent::createWidget($args); + } } diff --git a/ipf/form/field/multiplechoice.php b/ipf/form/field/multiplechoice.php index d4900e9..98af7c9 100644 --- a/ipf/form/field/multiplechoice.php +++ b/ipf/form/field/multiplechoice.php @@ -8,7 +8,7 @@ class IPF_Form_Field_MultipleChoice extends IPF_Form_Field_Choice { foreach ($value as $v) { $find = false; - foreach ($this->_choices as $name => $val) { + foreach ($this->choices as $name => $val) { if ($v==$val) { $find = true; break; diff --git a/ipf/form/form.php b/ipf/form/form.php index 9fab7b8..a79e6c5 100644 --- a/ipf/form/form.php +++ b/ipf/form/form.php @@ -20,12 +20,13 @@ abstract class IPF_Form implements Iterator function __construct($data=null, $extra=array()) { + $this->initFields($extra); + if ($data !== null) { $this->data = $data; $this->is_bound = true; } $this->initial = \PFF\Arr::get($extra, 'initial', array()); - $this->initFields($extra); } abstract protected function initFields($extra=array()); diff --git a/ipf/form/widget.php b/ipf/form/widget.php index 718a0ca..e561d61 100644 --- a/ipf/form/widget.php +++ b/ipf/form/widget.php @@ -2,9 +2,8 @@ abstract class IPF_Form_Widget { - public $is_hidden = false; + public $is_hidden = false; // renders invisible public $needs_multipart_form = false; - public $input_type = ''; public $attrs = array(); public function __construct($attrs=array()) diff --git a/ipf/form/widget/input.php b/ipf/form/widget/input.php index f3a8ee5..ae2a960 100644 --- a/ipf/form/widget/input.php +++ b/ipf/form/widget/input.php @@ -4,6 +4,8 @@ use \PFF\HtmlBuilder\Tag as Tag; class IPF_Form_Widget_Input extends IPF_Form_Widget { + public $input_type = ''; + public function render($name, $value, $extra_attrs=array()) { $tag = Tag::input()