Skip to content

Commit

Permalink
Now handles horizontal forms
Browse files Browse the repository at this point in the history
  • Loading branch information
mlantz committed Aug 21, 2017
1 parent f4e1872 commit bbc1f10
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
17 changes: 14 additions & 3 deletions config/form-maker.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@
return [

'form' => [
'form-class' => 'form-control',
'group-class' => 'form-group',
'error-class' => 'has-error',
'label-class' => 'control-label',

/*
* --------------------------------------------------------------------------
* For Horizontal forms
* --------------------------------------------------------------------------
* You may wish to use horizontal forms. If you do, you need to set the
* orientation to horizontal, and ensure that your form has the 'form-horizontal'
* class.
*/

'orientation' => 'vertical',
'label-column' => 'col-md-2',
'input-column' => 'col-md-10',
'checkbox-column' => 'col-md-offset-2 col-md-10',
],

'inputTypes' => [
Expand All @@ -36,6 +49,4 @@
'radio' => null,
'radio-inline' => null,
]

];

22 changes: 18 additions & 4 deletions src/Services/FormMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ public function getFormErrors()
*/
private function formBuilder($view, $errors, $field, $column, $input)
{
$formGroupClass = Config::get('form-maker.form.group-class', 'form-group');
$formErrorClass = Config::get('form-maker.form.error-class', 'has-error');
$formGroupClass = config('form-maker.form.group-class', 'form-group');
$formErrorClass = config('form-maker.form.error-class', 'has-error');

$errorHighlight = '';
$errorMessage = false;
Expand Down Expand Up @@ -304,18 +304,32 @@ private function formBuilder($view, $errors, $field, $column, $input)
*/
public function formContentBuild($field, $column, $input, $errorMessage)
{
$formLabelClass = Config::get('form-maker.form.label-class', 'control-label');
$labelColumn = $labelCheckableColumn = '';
$singleLineCheckType = false;
$formLabelClass = config('form-maker.form.label-class', 'control-label');

if (config('form-maker.form.orientation') == 'horizontal') {
$labelColumn = config('form-maker.form.label-column');
$labelCheckableColumn = config('form-maker.form.checkbox-column');
$singleLineCheckType = true;
}

$formBuild = '<label class="'.$formLabelClass.'" for="'.ucfirst($column).'">';
$formBuild = '<label class="'.$formLabelClass.' '.$labelColumn.'" for="'.ucfirst($column).'">';
$formBuild .= $this->inputUtilities->cleanString($this->columnLabel($field, $column));
$formBuild .= '</label>'.$input.$this->errorMessage($errorMessage);

if (isset($field['type'])) {
if (in_array($field['type'], ['radio', 'checkbox'])) {
$formBuild = '<div class="'.$field['type'].'">';
if ($singleLineCheckType) {
$formBuild .= '<div class="'.$labelCheckableColumn.'">';
}
$formBuild .= '<label for="'.ucfirst($column).'" class="'.$formLabelClass.'">'.$input;
$formBuild .= $this->inputUtilities->cleanString($this->columnLabel($field, $column));
$formBuild .= '</label>'.$this->errorMessage($errorMessage).'</div>';
if ($singleLineCheckType) {
$formBuild .= '</div>';
}
} elseif (stristr($field['type'], 'hidden')) {
$formBuild = $input;
}
Expand Down
33 changes: 20 additions & 13 deletions src/Services/InputMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ class InputMaker
],
];

protected $standardMethods = [
'makeHidden',
'makeText',
];

protected $selectedMethods = [
'makeSelected',
'makeCheckbox',
'makeRadio',
];

public function __construct()
{
$this->htmlGenerator = new HtmlGenerator();
Expand Down Expand Up @@ -126,6 +137,7 @@ public function inputStringPreparer($config)
{
$inputString = '';
$beforeAfterCondition = ($this->before($config) > '' || $this->after($config) > '');
$method = $this->getGeneratorMethod($config['inputType']);

if ($beforeAfterCondition) {
$inputString .= '<div class="input-group">';
Expand All @@ -139,6 +151,10 @@ public function inputStringPreparer($config)
$inputString .= '</div>';
}

if (config('form-maker.form.orientation') == 'horizontal' && !in_array($method, $this->selectedMethods)) {
return '<div class="'.config('form-maker.form.input-column', '').'">'.$inputString.'</div>';
}

return $inputString;
}

Expand Down Expand Up @@ -276,20 +292,11 @@ private function inputStringGenerator($config)
$custom = $this->inputCalibrator->getField($config, 'custom');
$method = $this->getGeneratorMethod($config['inputType']);

$standardMethods = [
'makeHidden',
'makeText',
];

$selectedMethods = [
'makeSelected',
'makeCheckbox',
'makeRadio',
];

if (in_array($method, $standardMethods)) {
if (in_array($method, $this->standardMethods)) {
$inputString = $this->htmlGenerator->$method($config, $population, $custom);
} elseif (in_array($method, $selectedMethods)) {
} elseif (in_array($method, $this->selectedMethods)) {
// add extra class

$inputString = $this->htmlGenerator->$method($config, $selected, $custom);
} elseif ($method === 'makeRelationship') {
$inputString = $this->htmlGenerator->makeRelationship(
Expand Down

0 comments on commit bbc1f10

Please sign in to comment.