final touches on Forms

system - removed trailing unused form-html
Form - set proper width for submit-button column
Element - support returning raw-list for Inputs
Group,Input - renamed getColumnWidth to getWidth
Input - make setWidth consistent with Form->setWidth, directly add
  proper class to column
This commit is contained in:
Sjon Hortensius 2015-01-27 21:28:17 +01:00
parent dc58b7b3e0
commit 69f9ff4034
8 changed files with 57 additions and 32 deletions

View File

@ -52,14 +52,18 @@ class Form extends Form_Element
public function __toString()
{
$sections = implode('', $this->_sections);
$submit = isset($this->_submit) ? '<div class="col-sm-offset-'. $this->_labelWidth .'">'. $this->_submit .'</div>' : '';
if (isset($this->_submit))
{
$this->_submit->setWidth(12 - $this->getLabelWidth());
$this->_submit->addColumnClass('col-sm-offset-'. $this->_labelWidth);
$sections .= $this->_submit;
}
return <<<EOT
<form class="form-horizontal" method="post">
{$sections}
{$submit}
</form>
EOT;
}
}
}

View File

@ -25,4 +25,4 @@ class Form_Checkbox extends Form_Input
return '<label>'. $input .' '. gettext($this->_description) .'</label>';
}
}
}

View File

@ -19,16 +19,21 @@ class Form_Element
return $this;
}
public function getHtmlClass()
public function getHtmlClass($wrapped = true)
{
if (empty($this->_classes))
return '';
return ' class="'. implode(' ', array_keys($this->_classes)).'" ';
$list = implode(' ', array_keys($this->_classes));
if (!$wrapped)
return $list;
return 'class="'. $list .'"';
}
protected function _setParent(Form_Element $parent)
{
$this->_parent = $parent;
}
}
}

View File

@ -29,7 +29,7 @@ class Form_Group extends Form_Element
$this->_labelTarget = $input;
}
// Potentially allow overloading
// Potentially allow overloading
public function getLabelWidth()
{
return $this->_parent->getLabelWidth();
@ -50,7 +50,7 @@ class Form_Group extends Form_Element
foreach ($this->_inputs as $input)
{
$width = $input->getColumnWidth();
$width = $input->getWidth();
if (isset($width))
$spaceLeft -= $width;
@ -76,4 +76,4 @@ class Form_Group extends Form_Element
</div>
EOT;
}
}
}

View File

@ -51,27 +51,48 @@ class Form_Input extends Form_Element
return $this;
}
public function getColumnWidth()
public function getWidth()
{
return $this->_columnWidth;
}
public function setWidth($count, $soft = false)
public function setWidth($size)
{
if ($soft && isset($this->_columnWidth))
return;
if ($size < 1 || $size > 12)
throw new Exception('Incorrect size, pass a number between 1 and 12');
$this->_columnWidth = (int)$count;
$this->removeColumnClass('col-sm-'. $this->_columnWidth);
$this->_columnWidth = (int)$size;
$this->addColumnClass('col-sm-'. $this->_columnWidth);
}
public function addColumnClass($class)
{
array_push($this->_columnClasses, $class);
$this->_columnClasses[$class] = true;
return $this;
}
public function removeColumnClass($class)
{
unset($this->_columnClasses[$class]);
return $this;
}
public function getColumnHtmlClass()
{
if (empty($this->_columnClasses))
return '';
return 'class="'. implode(' ', array_keys($this->_columnClasses)).'"';
}
protected function _getInput()
{
$html = '<input '. $this->getHtmlClass();
$html = '<input';
$attributes = $this->_attributes;
if (isset($this->_name))
@ -85,16 +106,17 @@ class Form_Input extends Form_Element
public function __toString()
{
$this->_attributes['class'] = $this->getHtmlClass(false);
$input = $this->_getInput();
$columnClasses = implode(' ', $this->_columnClasses);
$help = isset($this->_help) ? '<span class="help-block">'. gettext($this->_help). '</span>' : '';
return <<<EOT
<div class="col-sm-{$this->_columnWidth} {$columnClasses}">
<div {$this->getColumnHtmlClass()}>
{$input}
{$help}
</div>
EOT;
}
}
}

View File

@ -50,4 +50,4 @@ class Form_Section extends Form_Element
</div>
EOT;
}
}
}

View File

@ -16,7 +16,7 @@ class Form_Select extends Form_Input
protected function _getInput()
{
$element = 'select'. $this->getHtmlClass();
$element = 'select';
foreach ($this->_attributes as $key => $value)
$element .= ' '.$key.'="'. htmlspecialchars($value).'"';
@ -30,4 +30,4 @@ class Form_Select extends Form_Input
</select>
EOT;
}
}
}

View File

@ -395,11 +395,5 @@ $section->addInput(new Form_Select(
$form->add($section);
print $form;
return;
?>
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary"><?=gettext("Save");?></button>
</div>
</form>
</div>
<?php include("foot.inc"); ?>
include("foot.inc");