Skip to content
This repository has been archived by the owner on Feb 26, 2018. It is now read-only.

Commit

Permalink
Finished first of horizontal form builder
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Sep 20, 2013
1 parent c415af7 commit ef4cdc4
Show file tree
Hide file tree
Showing 14 changed files with 1,030 additions and 156 deletions.
104 changes: 86 additions & 18 deletions src/AdamWathan/BootForms/BasicFormBuilder.php
Expand Up @@ -2,45 +2,113 @@

use AdamWathan\Form\FormBuilder;
use AdamWathan\BootForms\Elements\FormGroup;
use Illuminate\Session\Store as Session;
use AdamWathan\BootForms\Elements\CheckGroup;
use AdamWathan\BootForms\Elements\HelpBlock;

class BasicFormBuilder
{

private $controlOptions = array('class' => 'form-control');

private $builder;
private $session;

public function __construct(FormBuilder $builder)
{
$this->builder = $builder;
}

protected function formGroup($label, $name, $control)
{
$label = $this->builder->label($label, $name)->addClass('control-label')->forId($name);
$control->id($name)->addClass('form-control');

$formGroup = new FormGroup($label, $control);

if ($this->builder->hasError($name)) {
$formGroup->helpBlock(new HelpBlock($this->builder->getError($name)));
$formGroup->addClass('has-error');
}

return $formGroup;
}

public function text($label, $name, $value = null)
{
$control = $this->builder->text($name)->value($value);

return $this->formGroup($label, $name, $control);
}

public function password($label, $name)
{
$control = $this->builder->password($name);

return $this->formGroup($label, $name, $control);
}

protected function formGroup($label, $control)
public function submit($type = "btn-default", $value = "Submit")
{
return new FormGroup($label, $control);
return $this->builder->submit($value)->addClass('btn')->addClass($type);
}

public function select($label, $name, $options = array())
{
$control = $this->builder->select($name, $options);

return $this->formGroup($label, $name, $control);
}

protected function getValidationClass($name)
public function checkbox($label, $name)
{
return '';
if (! $this->hasErrors() || ! $this->hasError($name)) {
return '';
$control = $this->builder->checkbox($name);

return $this->checkGroup($label, $name, $control)->addClass('checkbox');
}

protected function checkGroup($label, $name, $control)
{
$label = $this->builder->label($label, $name)->after($control)->addClass('control-label');

$checkGroup = new CheckGroup($label);

if ($this->builder->hasError($name)) {
$checkGroup->helpBlock(new HelpBlock($this->builder->getError($name)));
$checkGroup->addClass('has-error');
}
return 'has-error';

return $checkGroup;
}


public function text($label, $name, $value = null)
public function radio($label, $name, $value = null)
{
if (is_null($value)) {
$value = $label;
}

$control = $this->builder->radio($name, $value);

return $this->checkGroup($label, $name, $control)->addClass('radio');
}

public function textarea($label, $name)
{
$control = $this->builder->textarea($name);

return $this->formGroup($label, $name, $control);
}

public function inlineCheckbox($label, $name)
{
$label = $this->builder->label($label)->addClass('checkbox-inline');
$control = $this->builder->checkbox($name);

return $label->after($control);
}

public function inlineRadio($label, $name, $value = null)
{
$label = $this->builder->label($label, $name)->forId($name);
$control = $this->builder->text($name)->value($value)->id($name)->addClass('form-control');
$value = $value ?: $label;
$label = $this->builder->label($label)->addClass('radio-inline');
$control = $this->builder->radio($name, $value);

return $this->formGroup($label, $control);
return $label->after($control);
}

public function __call($method, $parameters)
Expand Down
8 changes: 4 additions & 4 deletions src/AdamWathan/BootForms/BootForm.php
Expand Up @@ -14,18 +14,18 @@ public function __construct(BasicFormBuilder $basicFormBuilder, HorizontalFormBu
}


public function open(array $options = array())
public function open()
{
$this->builder = $this->basicFormBuilder;
return $this->builder->open($options);
return $this->builder->open();
}

public function openHorizontal($labelWidth, $controlWidth, array $options = array())
public function openHorizontal($labelWidth, $controlWidth)
{
$this->horizontalFormBuilder->setLabelWidth($labelWidth);
$this->horizontalFormBuilder->setControlWidth($controlWidth);
$this->builder = $this->horizontalFormBuilder;
return $this->builder->open($options);
return $this->builder->open();
}

public function __call($method, $parameters)
Expand Down
8 changes: 2 additions & 6 deletions src/AdamWathan/BootForms/BootFormsServiceProvider.php
Expand Up @@ -18,31 +18,27 @@ class BootFormsServiceProvider extends ServiceProvider {
*/
public function register()
{
//
$this->registerBasicFormBuilder();
$this->registerHorizontalFormBuilder();
$this->registerBootForm();
}

// this should extend formbuilder instead of bootformbuilder
protected function registerBasicFormBuilder()
{
$this->app['bootform.basic'] = $this->app->share(function($app)
{
return new BasicFormBuilder($app['form'], $app['session']);
return new BasicFormBuilder($app['adamwathan.form']);
});
}

// this should extend formbuilder instead of bootformbuilder
protected function registerHorizontalFormBuilder()
{
$this->app['bootform.horizontal'] = $this->app->share(function($app)
{
return new HorizontalFormBuilder($app['form'], $app['session']);
return new HorizontalFormBuilder($app['adamwathan.form']);
});
}

// this should have the real generators injected
protected function registerBootForm()
{
$this->app['bootform'] = $this->app->share(function($app)
Expand Down
31 changes: 31 additions & 0 deletions src/AdamWathan/BootForms/Elements/CheckGroup.php
@@ -0,0 +1,31 @@
<?php namespace AdamWathan\BootForms\Elements;

use AdamWathan\Form\Elements\Element;
use AdamWathan\Form\Elements\Label;

class CheckGroup extends FormGroup
{
public function __construct(Label $label)
{
$this->label = $label;
}

public function render()
{
$html = '<div';
$html .= $this->renderAttributes();
$html .= '>';
$html .= $this->label;
$html .= $this->renderHelpBlock();

$html .= '</div>';

return $html;
}

public function __call($method, $parameters)
{
call_user_func_array(array($this->label->getControl(), $method), $parameters);
return $this;
}
}
36 changes: 23 additions & 13 deletions src/AdamWathan/BootForms/Elements/FormGroup.php
Expand Up @@ -2,17 +2,17 @@

use AdamWathan\Form\Elements\Element;
use AdamWathan\Form\Elements\Label;
use AdamWathan\Form\Elements\Input;

class FormGroup extends Element
{
private $label;
private $input;
protected $label;
protected $control;
protected $helpBlock;

public function __construct(Label $label, Input $input)
public function __construct(Label $label, Element $control)
{
$this->label = $label;
$this->input = $input;
$this->control = $control;
$this->addClass('form-group');
}

Expand All @@ -22,22 +22,32 @@ public function render()
$html .= $this->renderAttributes();
$html .= '>';
$html .= $this->label;
$html .= $this->input;


// Create a help block object and set that if there's an error from the FormBuilder itself
// if ($this->hasError($name)) {
// $html .= '<p class="help-block">' . $this->getError($name) . '</p>';
// }
$html .= $this->control;
$html .= $this->renderHelpBlock();

$html .= '</div>';

return $html;
}

public function helpBlock(HelpBlock $helpBlock)
{
$this->helpBlock = $helpBlock;
return $this;
}

protected function renderHelpBlock()
{
if ($this->helpBlock) {
return $this->helpBlock->render();
}

return '';
}

public function __call($method, $parameters)
{
call_user_func_array(array($this->input, $method), $parameters);
call_user_func_array(array($this->control, $method), $parameters);
return $this;
}
}
25 changes: 25 additions & 0 deletions src/AdamWathan/BootForms/Elements/HelpBlock.php
@@ -0,0 +1,25 @@
<?php namespace AdamWathan\BootForms\Elements;

use AdamWathan\Form\Elements\Element;

class HelpBlock extends Element
{
private $message;

public function __construct($message)
{
$this->message = $message;
$this->addClass('help-block');
}

public function render()
{
$html = '<p';
$html .= $this->renderAttributes();
$html .= '>';
$html .= $this->message;
$html .= '</p>';

return $html;
}
}
48 changes: 48 additions & 0 deletions src/AdamWathan/BootForms/Elements/HorizontalFormGroup.php
@@ -0,0 +1,48 @@
<?php namespace AdamWathan\BootForms\Elements;

use AdamWathan\Form\Elements\Element;
use AdamWathan\Form\Elements\Label;

class HorizontalFormGroup extends FormGroup
{
protected $controlWidth;

public function __construct(Label $label, Element $control, $controlWidth = 10)
{
parent::__construct($label, $control);
$this->controlWidth = $controlWidth;
}

public function render()
{
$html = '<div';
$html .= $this->renderAttributes();
$html .= '>';
$html .= $this->label;
$html .= '<div class="' . $this->getControlClass() . '">';
$html .= $this->control;
$html .= $this->renderHelpBlock();
$html .= '</div>';

$html .= '</div>';

return $html;
}

public function setControlWidth($width)
{
$this->controlWidth = $width;
return $this;
}

protected function getControlClass()
{
return 'col-lg-' . $this->controlWidth;
}

public function __call($method, $parameters)
{
call_user_func_array(array($this->control, $method), $parameters);
return $this;
}
}

0 comments on commit ef4cdc4

Please sign in to comment.