adding app/library/Form.php
This commit is contained in:
parent
316edec020
commit
ddb9d8934d
1 changed files with 70 additions and 0 deletions
70
app/library/Form.php
Normal file
70
app/library/Form.php
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Httpcb;
|
||||
|
||||
use Phalcon\Forms\Form as FormBase,
|
||||
Phalcon\Forms\Element as FormElement;
|
||||
|
||||
class Form extends FormBase
|
||||
{
|
||||
public function renderDecorated($name, $opt = [])
|
||||
{
|
||||
$options = [
|
||||
'label-class' => '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(
|
||||
'<label class="%s" for="%s">%s</label>',
|
||||
$opt['label-class'], $ele->getName(), $ele->getLabel());
|
||||
}
|
||||
|
||||
$xhtml .= '<div class="' . implode(' ', $opt['class']) . '">'
|
||||
. $ele->render();
|
||||
|
||||
if (strlen($opt['message']) > 0) {
|
||||
$xhtml .= '<span class="help-block">' . $opt['message'] . '</span>';
|
||||
}
|
||||
|
||||
$xhtml .= '</div>';
|
||||
|
||||
return $xhtml;
|
||||
}
|
||||
}
|
||||
Reference in a new issue