]> git.andy128k.dev Git - ipf.git/commitdiff
form labels
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 19 Aug 2014 19:35:36 +0000 (22:35 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 19 Aug 2014 19:35:36 +0000 (22:35 +0300)
ipf/admin/form.php
ipf/form.php
ipf/form/boundfield.php
ipf/form/layout.php

index 03bedbbd1ad01258a698f4a7a4d9b5a6e2c24b2d..3f7330a8b6d37eef6c7c95c3a4670080c485ab3e 100644 (file)
@@ -40,7 +40,7 @@ class IPF_Admin_Form_Layout extends IPF_Form_LayoutAdapter
             return '';
     }
 
-    public function field($boundField, $label)
+    public function field($boundField)
     {
         if ($boundField->help_text) {
             $help_text = Tag::p(array('class' => 'help'))
@@ -52,9 +52,9 @@ class IPF_Admin_Form_Layout extends IPF_Form_LayoutAdapter
         return $this->errors($boundField->errors) .
             Tag::div(array('class' => 'form-row'),
                 Tag::div()
-                    ->raw($label)
+                    ->raw($boundField->renderLabel())
                     ->raw(' ')
-                    ->raw($boundField->render())
+                    ->raw($boundField->renderWidget())
                     ->append($help_text));
     }
 
@@ -66,7 +66,7 @@ class IPF_Admin_Form_Layout extends IPF_Form_LayoutAdapter
         $ul = Tag::ul(array('class' => 'errorlist'));
         foreach ($errors as $err)
             $ul->append(Tag::li(null, $err));
-        return Tag::div(null, $ul);
+        return Tag::div(array('class' => 'form-row'), $ul);
     }
 }
 
index 2770c08d5cac85602d6b91c5547f95b1ee5e20b7..f7d9d73b95a3d6f90e71752b4ff50397140a1a40 100644 (file)
@@ -165,23 +165,9 @@ abstract class IPF_Form implements Iterator
                 if ($field->widget->is_hidden)
                     continue;
 
-                $bf = new IPF_Form_BoundField($this, $field, $name);
-
-                $label = $field->label;
-                if ($this->label_suffix) {
-                    if (!in_array(mb_substr($label, -1, 1),
-                                array(':','?','.','!'))) {
-                        $label .= $this->label_suffix;
-                    }
-                }
-
-                if ($field->required)
-                    $label_attrs = array('class'=>'required');
-                else
-                    $label_attrs = null;
-                $label = $bf->labelTag($label, $label_attrs, true);
+                $bf = $this->field($name);
 
-                $output .= $layout->field($bf, $label);
+                $output .= $layout->field($bf);
             }
             $output .= $layout->endGroup($groupLabel);
         }
index db47a3dc48aeaa6c56058e79226190525a79df21..0a5e3ec85a29c6785181002d0d8295cc5a04a55e 100644 (file)
@@ -18,11 +18,16 @@ class IPF_Form_BoundField
         $this->field = $field;
         $this->name = $name;
         $this->html_name = $this->form->addPrefix($name);
-        if ($this->field->label == '') {
-            $this->label = mb_ereg_replace('/\_/', '/ /', mb_ucfirst($name));
-        } else {
+
+        if ($this->field->label) {
             $this->label = $this->field->label;
+        } else {
+            $this->label = mb_ereg_replace('/\_/', '/ /', mb_ucfirst($name));
+        }
+        if ($this->form->label_suffix && !in_array(mb_substr($this->label, -1, 1), array(':', '?', '.', '!'))) {
+            $this->label .= $this->form->label_suffix;
         }
+
         $this->help_text = ($this->field->help_text) ? $this->field->help_text : '';
         if (isset($this->form->errors[$name])) {
             $this->errors = $this->form->errors[$name];
@@ -37,52 +42,32 @@ class IPF_Form_BoundField
             return $this->form->initial($this->name);
     }
 
-    public function render()
+    public function renderWidget()
     {
         $widget = $this->field->widget;
 
         $extra_attrs = array();
-        if (array_key_exists('id', $widget->attrs)) {
+        if (!array_key_exists('id', $widget->attrs)) {
             $extra_attrs['id'] = $this->autoId();
         }
 
         return $widget->render($this->html_name, $this->value(), $extra_attrs);
     }
 
-    public function render_w($widget=null, $attrs=array())
-    {
-        if ($widget === null) {
-            $widget = $this->field->widget;
-        }
-        $id = $this->autoId();
-        if ($id and !array_key_exists('id', $attrs)
-            and !array_key_exists('id', $widget->attrs)) {
-            $attrs['id'] = $id;
-        }
-
-        $code = $widget->render($this->html_name, $this->value(), $attrs);
-
-        return $this->form->unescape($code);
-    }
-
-    public function labelTag($contents=null, $attrs=null, $raw=false)
+    public function renderLabel()
     {
-        $label = Tag::label($attrs);
-
-        if ($contents)
-            $label->raw($contents);
-        else
-            $label->append($this->label);
+        $label = Tag::label()
+            ->append($this->label);
 
         $widget = $this->field->widget;
         $id = (isset($widget->attrs['id'])) ? $widget->attrs['id'] : $this->autoId();
 
         $label->attr('for', $widget->idForLabel($id));
 
-        if ($raw)
-            return $label->html();
-        else
-            return $this->form->unescape($label->html());
+        if ($this->field->required)
+            $label->addClass('required');
+
+        return $label->html();
     }
 
     public function autoId()
@@ -96,6 +81,17 @@ class IPF_Form_BoundField
         return '';
     }
 
+
+    public function labelTag()
+    {
+        return $this->form->unescape($this->renderLabel());
+    }
+
+    public function render_w()
+    {
+        return $this->form->unescape($this->renderWidget());
+    }
+
     public function fieldErrors($raw=false)
     {
         $errors = IPF_Form::renderErrorsAsHTML($this->errors);
index 8d87642412311defb1ad48ae5eddee9549745940..89a6eeade54afa2702c810bc73cf61065888dc04 100644 (file)
@@ -4,7 +4,7 @@
 {
     public function startForm($form);
     public function startGroup($label);
-    public function field($boundField, $label);
+    public function field($boundField);
     public function endGroup($label);
     public function endForm($form);
 }*/
@@ -14,7 +14,7 @@ abstract class IPF_Form_LayoutAdapter /*implements informal interface IPF_Form_L
 {
     public function startForm($form) { return ''; }
     public function startGroup($label) { return ''; }
-    public abstract function field($boundField, $label);
+    public abstract function field($boundField);
     public function endGroup($label) { return ''; }
     public function endForm($form) { return ''; }
 
@@ -31,7 +31,7 @@ abstract class IPF_Form_LayoutAdapter /*implements informal interface IPF_Form_L
     {
         $hiddenWidgets = '';
         foreach ($form->hiddenFields() as $field_name)
-            $hiddenWidgets .= $form->field($field_name)->render();
+            $hiddenWidgets .= $form->field($field_name)->renderWidget();
         return $hiddenWidgets;
     }
 
@@ -75,14 +75,14 @@ class IPF_Form_ParagraphLayout extends IPF_Form_LayoutAdapter
         return $o;
     }
 
-    public function field($boundField, $label)
+    public function field($boundField)
     {
         return
             $this->errorList($boundField->errors) .
             Tag::p()
-                ->raw($label)
+                ->raw($boundField->renderLabel())
                 ->raw(' ')
-                ->raw($boundField->render())
+                ->raw($boundField->renderWidget())
                 ->raw(' ')
                 ->raw($boundField->help_text);
     }
@@ -104,7 +104,7 @@ class IPF_Form_ListLayout extends IPF_Form_LayoutAdapter
             return '';
     }
 
-    public function field($boundField, $label)
+    public function field($boundField)
     {
         if ($boundField->help_text)
             $help_text = '<br /><span class="helptext">'.$boundField->help_text.'</span>';
@@ -114,9 +114,9 @@ class IPF_Form_ListLayout extends IPF_Form_LayoutAdapter
         return Tag::li()
             ->raw($this->takeDeferred())
             ->raw($this->errorList($boundField->errors))
-            ->raw($label)
+            ->raw($boundField->renderLabel())
             ->raw(' ')
-            ->raw($boundField->render())
+            ->raw($boundField->renderWidget())
             ->raw($help_text);
     }
 
@@ -147,7 +147,7 @@ class IPF_Form_TableLayout extends IPF_Form_LayoutAdapter
             return '';
     }
 
-    public function field($boundField, $label)
+    public function field($boundField)
     {
         if ($boundField->help_text)
             $help_text = '<br /><span class="helptext">'.$boundField->help_text.'</span>';
@@ -156,11 +156,11 @@ class IPF_Form_TableLayout extends IPF_Form_LayoutAdapter
 
         return Tag::tr(null,
             Tag::th()
-                ->raw($label),
+                ->raw($boundField->renderLabel()),
             Tag::td()
                 ->raw($this->takeDeferred())
                 ->raw($this->errorList($boundField->errors))
-                ->raw($boundField->render())
+                ->raw($boundField->renderWidget())
                 ->raw($help_text));
     }