From 4fd6b12e02e31c7f86a341a00ec6afd4704f7576 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Tue, 19 Aug 2014 22:35:36 +0300 Subject: [PATCH] form labels --- ipf/admin/form.php | 8 +++--- ipf/form.php | 18 ++----------- ipf/form/boundfield.php | 60 +++++++++++++++++++---------------------- ipf/form/layout.php | 24 ++++++++--------- 4 files changed, 46 insertions(+), 64 deletions(-) diff --git a/ipf/admin/form.php b/ipf/admin/form.php index 03bedbb..3f7330a 100644 --- a/ipf/admin/form.php +++ b/ipf/admin/form.php @@ -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); } } diff --git a/ipf/form.php b/ipf/form.php index 2770c08..f7d9d73 100644 --- a/ipf/form.php +++ b/ipf/form.php @@ -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); } diff --git a/ipf/form/boundfield.php b/ipf/form/boundfield.php index db47a3d..0a5e3ec 100644 --- a/ipf/form/boundfield.php +++ b/ipf/form/boundfield.php @@ -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); diff --git a/ipf/form/layout.php b/ipf/form/layout.php index 8d87642..89a6eea 100644 --- a/ipf/form/layout.php +++ b/ipf/form/layout.php @@ -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 = '
'.$boundField->help_text.''; @@ -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 = '
'.$boundField->help_text.''; @@ -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)); } -- 2.49.0