From ddb9d8934dcb1b99e660f70bf7d16b2d354b4592 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 14 Aug 2018 17:31:07 +0200 Subject: [PATCH] adding app/library/Form.php --- app/library/Form.php | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 app/library/Form.php diff --git a/app/library/Form.php b/app/library/Form.php new file mode 100644 index 0000000..ebbb8bc --- /dev/null +++ b/app/library/Form.php @@ -0,0 +1,70 @@ + 'control-label', + 'class' => [ 'col-sm-10' ], + 'message' => '' + ]; + + $ele = $this->get($name); + + if (isset($opt['label-length'])) { + $length = (int) $opt['label-length']; + } else { + $length = 2; + } + $options['label-class'] .= ' col-sm-' . $length; + + if (isset($opt['length'])) { + + $len = $opt['length']; + + if ($len === 'full') { + $options['class'] = []; + } else { + $options['class'] = [ 'col-sm-' . $len ]; + } + + unset($opt['length']); + } + + if ($ele->hasMessages()) { + $options['class'][] = 'has-error'; + $options['message'] = $ele->getMessages()->current(); + } + + return $this->_render($ele, $options); + } + + protected function _render(FormElement $ele, $opt) + { + $xhtml = ''; + + if (strlen($ele->getLabel()) > 0) { + + $xhtml .= sprintf( + '', + $opt['label-class'], $ele->getName(), $ele->getLabel()); + } + + $xhtml .= '
' + . $ele->render(); + + if (strlen($opt['message']) > 0) { + $xhtml .= '' . $opt['message'] . ''; + } + + $xhtml .= '
'; + + return $xhtml; + } +}