From: Andrey Kutejko Date: Tue, 26 Aug 2014 22:03:13 +0000 (+0300) Subject: cleanup forms X-Git-Tag: 0.6~157 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=b0e11dcfdcd92898d8cd9cc98ad96b8349b8cb3e;p=ipf.git cleanup forms --- diff --git a/ipf/form.php b/ipf/form.php index 39e169b..9fab7b8 100644 --- a/ipf/form.php +++ b/ipf/form.php @@ -40,6 +40,9 @@ abstract class IPF_Form implements Iterator function isValid() { + if (!$this->is_bound) + return false; + if ($this->is_valid !== null) return $this->is_valid; @@ -47,8 +50,8 @@ abstract class IPF_Form implements Iterator $this->errors = array(); $form_methods = get_class_methods($this); - foreach ($this->fields as $name=>$field) { - $value = $field->widget->valueFromFormData($this->addPrefix($name), $this->data); + foreach ($this->fields as $name => $field) { + $value = $this->field($name)->value(); try { $value = $field->clean($value); $this->cleaned_data[$name] = $value; @@ -212,9 +215,8 @@ abstract class IPF_Form implements Iterator public function current() { - $field = current($this->fields); $name = key($this->fields); - return new IPF_Form_BoundField($this, $field, $name); + return $this->field($name); } public function key() diff --git a/ipf/form/boundfield.php b/ipf/form/boundfield.php index 0a5e3ec..ce18a79 100644 --- a/ipf/form/boundfield.php +++ b/ipf/form/boundfield.php @@ -37,7 +37,7 @@ class IPF_Form_BoundField public function value() { if ($this->form->is_bound) - return $this->field->widget->valueToFormData($this->html_name, $this->form->data); + return $this->field->widget->valueFromFormData($this->html_name, $this->form->data); else return $this->form->initial($this->name); } diff --git a/ipf/form/widget.php b/ipf/form/widget.php index f58c5d0..718a0ca 100644 --- a/ipf/form/widget.php +++ b/ipf/form/widget.php @@ -19,25 +19,9 @@ abstract class IPF_Form_Widget return array(); } - protected function buildAttrs($attrs, $extra_attrs=array()) + public function valueFromFormData($name, $data) { - return array_merge($this->attrs, $attrs, $extra_attrs); - } - - public function valueFromFormData($name, &$data) - { - if (isset($data[$name])) { - return $data[$name]; - } - return null; - } - - public function valueToFormData($name, $data) - { - if (isset($data[$name])) { - return $data[$name]; - } - return null; + return isset($data[$name]) ? $data[$name] : null; } public function idForLabel($id) diff --git a/ipf/form/widget/checkboxinput.php b/ipf/form/widget/checkboxinput.php index f147a76..78a9c87 100644 --- a/ipf/form/widget/checkboxinput.php +++ b/ipf/form/widget/checkboxinput.php @@ -13,7 +13,7 @@ class IPF_Form_Widget_CheckboxInput extends IPF_Form_Widget_Input return parent::render($name, '', $extra_attrs); } - public function valueFromFormData($name, &$data) + public function valueFromFormData($name, $data) { return isset($data[$name]) && false !== $data[$name] && (string)$data[$name] !== '0' && (string)$data[$name] !== 'off'; } diff --git a/ipf/form/widget/dateinput.php b/ipf/form/widget/dateinput.php index a2ac015..40af6b1 100644 --- a/ipf/form/widget/dateinput.php +++ b/ipf/form/widget/dateinput.php @@ -16,12 +16,15 @@ class IPF_Form_Widget_DateInput extends IPF_Form_Widget_Input public function render($name, $value, $extra_attrs=array()) { + if ($value) + $value = IPF_Format::formatDate($this->format, strtotime($value)); + $extra_attrs['class'] = 'dateinput'; $extra_attrs['data-format'] = IPF_Format::makeDateFormat($this->format, IPF_Format::$pickerControls); return parent::render($name, $value, $extra_attrs); } - public function valueFromFormData($name, &$data) + public function valueFromFormData($name, $data) { if (!isset($data[$name]) || !$data[$name]) { return null; @@ -37,14 +40,5 @@ class IPF_Form_Widget_DateInput extends IPF_Form_Widget_Input return null; } } - - public function valueToFormData($name, $data) - { - if (isset($data[$name]) && $data[$name]) { - return IPF_Format::formatDate($this->format, strtotime($data[$name])); - } else { - return null; - } - } } diff --git a/ipf/form/widget/datetimeinput.php b/ipf/form/widget/datetimeinput.php index c390cb6..6fa1c7f 100644 --- a/ipf/form/widget/datetimeinput.php +++ b/ipf/form/widget/datetimeinput.php @@ -16,13 +16,16 @@ class IPF_Form_Widget_DatetimeInput extends IPF_Form_Widget_Input public function render($name, $value, $extra_attrs=array()) { + if ($value) + $value = IPF_Format::formatDate($this->format, strtotime($value.' GMT')); + $extra_attrs['class'] = 'datetimeinput'; $extra_attrs['data-dateformat'] = IPF_Format::makeDateFormat($this->format, IPF_Format::$pickerControls); $extra_attrs['data-timeformat'] = IPF_Format::makeTimeFormat($this->format, IPF_Format::$pickerControls); return parent::render($name, $value, $extra_attrs); } - public function valueFromFormData($name, &$data) + public function valueFromFormData($name, $data) { if (!isset($data[$name]) || !$data[$name]) { return null; @@ -43,15 +46,5 @@ class IPF_Form_Widget_DatetimeInput extends IPF_Form_Widget_Input return null; } } - - public function valueToFormData($name, $data) - { - if (isset($data[$name]) && $data[$name]) { - // Internally we use GMT, so we convert back to the current timezone. - return IPF_Format::formatDate($this->format, strtotime($data[$name].' GMT')); - } else { - return null; - } - } } diff --git a/ipf/form/widget/fileinput.php b/ipf/form/widget/fileinput.php index 321accf..eaa29d9 100644 --- a/ipf/form/widget/fileinput.php +++ b/ipf/form/widget/fileinput.php @@ -52,7 +52,7 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input return $sim . parent::render($name, '', $extra_attrs); } - public function valueFromFormData($name, &$data) + public function valueFromFormData($name, $data) { if (!isset($data[$name])) return null; @@ -72,13 +72,5 @@ class IPF_Form_Widget_FileInput extends IPF_Form_Widget_Input return $res; } - - public function valueToFormData($name, $data) - { - if (!isset($data[$name])) - return null; - $remove = isset($data[$name.'_remove']) && $data[$name.'_remove'] == 1; - return array('data'=>$data[$name], 'remove'=>$remove); - } } diff --git a/ipf/form/widget/htmlinput.php b/ipf/form/widget/htmlinput.php index cc488e4..b255a53 100644 --- a/ipf/form/widget/htmlinput.php +++ b/ipf/form/widget/htmlinput.php @@ -4,51 +4,45 @@ use \PFF\HtmlBuilder\Tag as Tag; class IPF_Form_Widget_HTMLInput extends IPF_Form_Widget { - public $mode = 'textareas'; - public $theme = 'simple'; - public $include_tinymce = true; - public $force_absolute_urls = false; + public $tinymce_url; + public $mode; + public $theme; + public $include_tinymce; + public $force_absolute_urls; + public $editor_config; public function __construct($attrs=array()) { - $defaults = array('cols' => '70', - 'rows' => '20'); - $config = array('tinymce_url', 'mode', 'theme', 'include_tinymce', 'force_absolute_urls'); - foreach ($config as $cfg) { - if (isset($attrs[$cfg])) { - $this->$cfg = $attrs[$cfg]; - unset($attrs[$cfg]); - } - } - $this->attrs = array_merge($defaults, $attrs); + $this->tinymce_url = \PFF\Arr::pop($attrs, 'tinymce_url'); + $this->mode = \PFF\Arr::pop($attrs, 'mode', 'textareas'); + $this->theme = \PFF\Arr::pop($attrs, 'theme', 'simple'); + $this->include_tinymce = \PFF\Arr::pop($attrs, 'include_tinymce', true); + $this->force_absolute_urls = \PFF\Arr::pop($attrs, 'force_absolute_urls', false); + $this->editor_config = \PFF\Arr::pop($attrs, 'editor_config', array()); + + parent::__construct($attrs); } public function render($name, $value, $extra_attrs=array()) { if ($value === null) $value = ''; + $extra_config = ''; - if (isset($this->attrs['editor_config'])) { - $_ec = $this->attrs['editor_config']; - unset($this->attrs['editor_config']); - $_st = array(); - foreach ($_ec as $key=>$val) { - if (is_bool($val)) { - if ($val) { - $_st[] = $key.' : true'; - } else { - $_st[] = $key.' : false'; - } - } else { - $_st[] = $key.' : "'.$val.'"'; - } - } - if ($_st) { - $extra_config = ",\n".implode(",\n", $_st); + foreach ($this->editor_config as $key => $val) { + $extra_config .= ",\n{$key} : "; + if (is_bool($val)) { + $extra_config .= $val ? 'true' : 'false'; + } else { + $extra_config .= '"'.$val.'"'; } } - $final_attrs = $this->buildAttrs(array('name' => $name), $extra_attrs); return Tag::textarea($final_attrs) + ->attr('cols', 70) + ->attr('rows', 20) + ->attrs($this->attrs) + ->attrs($extra_attrs) + ->attr('name', $name) ->addClass($this->force_absolute_urls ? 'htmlEditorAbs' : 'htmlEditor') ->append($value) ->html(); diff --git a/ipf/form/widget/input.php b/ipf/form/widget/input.php index 9287c0f..f3a8ee5 100644 --- a/ipf/form/widget/input.php +++ b/ipf/form/widget/input.php @@ -6,13 +6,16 @@ class IPF_Form_Widget_Input extends IPF_Form_Widget { public function render($name, $value, $extra_attrs=array()) { - $final_attrs = $this->buildAttrs(array('name' => $name, - 'type' => $this->input_type), - $extra_attrs); + $tag = Tag::input() + ->attrs($this->attrs) + ->attrs($extra_attrs) + ->attr('name', $name) + ->attr('type', $this->input_type); + if ($value) - $final_attrs['value'] = $value; + $tag->attr('value', $value); - return Tag::input($final_attrs); + return $tag->html(); } } diff --git a/ipf/form/widget/radioinput.php b/ipf/form/widget/radioinput.php index 56a9d6f..e6b4997 100644 --- a/ipf/form/widget/radioinput.php +++ b/ipf/form/widget/radioinput.php @@ -15,29 +15,28 @@ class IPF_Form_Widget_RadioInput extends IPF_Form_Widget parent::__construct($attrs); } - public function render($name, $value, $extra_attrs=array(), $choices=array()) + public function render($name, $value, $extra_attrs=array()) { - $output = array(); - if ($value === null) { + if ($value === null) $value = ''; - } - $output = Tag::ul(); // ->attrs($this->buildAttrs($extra_attrs)); - $choices = $this->choices + $choices; - $index = 1; - foreach ($choices as $option_label => $option_value) { - $optionId = "id_$name-$index"; - $radio = Tag::input(array('type' => 'radio', 'name' => $name, 'id' => $optionId, 'value' => $option_value)); + $output = Tag::ul() + ->attrs($this->attrs) + ->attrs($extra_attrs); + foreach ($this->choices as $option_label => $option_value) { + $radio = Tag::input(array('type' => 'radio', 'name' => $name, 'value' => $option_value)); if ($option_value == $value) $radio->attr('checked', 'checked'); $output->append(Tag::li(null, - $radio, - Tag::label(array('for' => $optionId), $option_label))); - - $index++; + Tag::label(null, $radio, ' ', $option_label))); } return $output->html(); } + + public function idForLabel($id) + { + return null; + } } diff --git a/ipf/form/widget/selectmultipleinput.php b/ipf/form/widget/selectmultipleinput.php index 2d2f55b..0b36c23 100644 --- a/ipf/form/widget/selectmultipleinput.php +++ b/ipf/form/widget/selectmultipleinput.php @@ -35,7 +35,7 @@ class IPF_Form_Widget_SelectMultipleInput extends IPF_Form_Widget return $select->html(); } - public function valueFromFormData($name, &$data) + public function valueFromFormData($name, $data) { if (isset($data[$name]) and is_array($data[$name])) { return $data[$name]; diff --git a/ipf/form/widget/selectmultipleinputcheckbox.php b/ipf/form/widget/selectmultipleinputcheckbox.php index 612f0f7..c818686 100644 --- a/ipf/form/widget/selectmultipleinputcheckbox.php +++ b/ipf/form/widget/selectmultipleinputcheckbox.php @@ -4,39 +4,34 @@ use \PFF\HtmlBuilder\Tag as Tag; class IPF_Form_Widget_SelectMultipleInputCheckbox extends IPF_Form_Widget_SelectMultipleInput { - public function render($name, $value, $extra_attrs=array(), $choices=array()) + public function render($name, $value, $extra_attrs=array()) { if ($value === null || $value == '') $value = array(); - $final_attrs = $this->buildAttrs($extra_attrs); - $output = Tag::ul(); - $choices = array_merge($this->choices, $choices); - $i=0; - $base_id = $final_attrs['id']; - foreach ($choices as $option_label => $option_value) { + + $output = Tag::ul() + ->attrs($this->attrs) + ->attrs($extra_attrs); + foreach ($this->choices as $option_label => $option_value) { $selected = in_array($option_value, $value); $checkbox = Tag::input($final_attrs) ->attr('type', 'checkbox') ->attr('name', "{$name}[]") - ->attr('id', $base_id.'_'.$i) ->attr('value', $option_value); + if ($selected) $checkbox->attr('checked', 'checked'); $output->append(Tag::li(null, Tag::label(null, $checkbox, ' ', $option_label))); - $i++; } return $output->html(); } public function idForLabel($id) { - if ($id) { - $id += '_0'; - } - return $id; + return null; } } diff --git a/ipf/form/widget/textareainput.php b/ipf/form/widget/textareainput.php index 876c44f..5117400 100644 --- a/ipf/form/widget/textareainput.php +++ b/ipf/form/widget/textareainput.php @@ -4,17 +4,19 @@ use \PFF\HtmlBuilder\Tag as Tag; class IPF_Form_Widget_TextareaInput extends IPF_Form_Widget { - public function __construct($attrs=array()) - { - $this->attrs = array_merge(array('cols' => '40', 'rows' => '10'), $attrs); - } - public function render($name, $value, $extra_attrs=array()) { if ($value === null) $value = ''; - $final_attrs = $this->buildAttrs(array('name' => $name), $extra_attrs); - return Tag::textarea($final_attrs, $value)->html(); + + return Tag::textarea() + ->attr('cols', 40) + ->attr('rows', 10) + ->attrs($this->attrs) + ->attrs($extra_attrs) + ->attr('name', $name) + ->append($value) + ->html(); } }