Created
August 5, 2012 18:47
-
-
Save shakyShane/3266639 to your computer and use it in GitHub Desktop.
Twitter Bootstrap Form Generator PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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