Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Forked from shakyShane/BootstrapForm.php
Created February 16, 2014 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/9027939 to your computer and use it in GitHub Desktop.
Save xeoncross/9027939 to your computer and use it in GitHub Desktop.
<?php
/**
*
* Helper Class to output correct Twitter Bootstrap Markup.
* Only option for now is the horizontal Form.
*
* Usage :
*
$form = new BootstrapForm('add_user');
$form->addInput('text-input', 'user_name', 'Your name:', true);
$form->addInput('text-area', 'user_address', 'Your Address', true);
return $form->outputForm();
*
*/
class BootstrapForm
{
var $output;
var $formId;
var $formClasses;
var $formType;
public function __construct($formId, $formClasses = null, $type = 'form-horizontal'){
$this->formId = $formId;
$this->formClasses = $formClasses;
$this->formType = $type;
$this->openForm();
}
public function openForm(){
$this->output = sprintf(('
<form id="%1$s" class="%1$s %3$s">
<fieldset>'),
$this->formId,
$this->formClasses,
$this->formType
);
}
public function closeForm(){
$this->output .=
'<div class="form-actions">
<button type = "submit" class="btn btn-primary"> Send</button>
<button class="btn"> Cancel</button>
</div >
</fieldset >
</form >';
}
public function addInput($type, $id, $label, $required = false, $extraClasses = false){
//TODO - Add all the other input types
switch ($type) {
case 'text-input' :
$string = '<input id="%1$s" class="span3 %2$s %3$s" onkeyup="" type="text" name="%1$s" value="" size="25">';
break;
case 'text-area' :
$string = '<textarea id="%1$s" name="%1$s" cols="45" rows="6" class="%2$s %3$s"></textarea>';
break;
}
//start the group
$this->output .= sprintf(('
<div class="control-group">
<label class="control-label" for="%1$s">%2$s</label>
<div class="controls">'), $id, $label);
//add the input element
$this->output .= sprintf(($string), $id, ($required) ? 'required' : null, ($extraClasses) ? $extraClasses : null);
//close the group
$this->output .= '</div></div>';
}
public function outputForm(){
$this->closeForm();
return $this->output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment