Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.