]> git.andy128k.dev Git - ipf.git/commitdiff
cleanup forms
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 26 Aug 2014 22:03:13 +0000 (01:03 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 26 Aug 2014 22:03:13 +0000 (01:03 +0300)
13 files changed:
ipf/form.php
ipf/form/boundfield.php
ipf/form/widget.php
ipf/form/widget/checkboxinput.php
ipf/form/widget/dateinput.php
ipf/form/widget/datetimeinput.php
ipf/form/widget/fileinput.php
ipf/form/widget/htmlinput.php
ipf/form/widget/input.php
ipf/form/widget/radioinput.php
ipf/form/widget/selectmultipleinput.php
ipf/form/widget/selectmultipleinputcheckbox.php
ipf/form/widget/textareainput.php

index 39e169b3ab2b4193d224e80121e6d8f42c289ee3..9fab7b80cabee0eea4694c615a9ff084bb4638ca 100644 (file)
@@ -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()
index 0a5e3ec85a29c6785181002d0d8295cc5a04a55e..ce18a799ec53fa453857eacb79db9095e139882e 100644 (file)
@@ -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);
     }
index f58c5d02baabb54ccf6605af409578554e251a6a..718a0ca96dc54d1213bad3cd8b84c8ad9b30b002 100644 (file)
@@ -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)
index f147a76a3ec342c15dfe8ba2bb32aebb88875a14..78a9c8727faeca47bc8c5f364ec3ca487f7bd72b 100644 (file)
@@ -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';
     }
index a2ac015e440196877a9aba9d72400c44b18fa872..40af6b1c7069aaa7cf0865bd81f2671174b2d543 100644 (file)
@@ -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;
-        }
-    }
 }
 
index c390cb6ce56476ed20c0bf6839f3fa1adea74e74..6fa1c7f33888d984c897fc1b7814ff6e8d6ab707 100644 (file)
@@ -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;
-        }
-    }
 }
 
index 321accfe660d2b9d530643c7231c84074f8888b5..eaa29d9c6a09d9609aaffd6f0110a32d010516ef 100644 (file)
@@ -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);
-    }
 }
 
index cc488e45c31e5363cb9ae69a84f03d760143b09f..b255a53629d1467724bcf689ebaf9605aa7a8164 100644 (file)
@@ -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();
index 9287c0f3ccf67d13a53203afa74d76048cb33bf5..f3a8ee5eb7e362a93dac4fd7e60f9f7c926c3fa5 100644 (file)
@@ -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();
     }
 }
 
index 56a9d6fe6dd0fbebd5e8ef0bc9f2f5cbbb3d41aa..e6b499735ed93bb7efc8259f35a17326d6538f5f 100644 (file)
@@ -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;
+    }
 }
 
index 2d2f55bdde538643ba23b27d978c271bd0f8d111..0b36c2368ee8eb3d31df1a2581fdf9d049214c4e 100644 (file)
@@ -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];
index 612f0f7c1297f78f24347310518b31d2c7fd9b61..c818686659770cc617fdee6bdfc0ce01e87ffecc 100644 (file)
@@ -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;
     }
 }
 
index 876c44ff73cef8ef7f23def64813b4f3c2442133..511740043536c03562492bb98ea0900856f4d821 100644 (file)
@@ -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();
     }
 }