Created
January 10, 2013 21:32
-
-
Save jtarleton/4505982 to your computer and use it in GitHub Desktop.
Helpers to render Bootstrap HTML for a symfony sfFormField object
This file contains hidden or 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 | |
/* | |
Helpers to render Bootstrap HTML for a symfony sfFormField object. | |
default input type is text | |
pass an array of field/widget names for each of the other different input types | |
*/ | |
function tbsFormRender($sfForm, $dateFields = array(), $selectFields = array(), $textareaFields = array(), $fileFields=array(), $extraElements = array()) | |
{ | |
?> | |
<?php if($sfForm->isBound()): /* Show form fields with the Boostrap Succss/Error styles */ ?> | |
<?php foreach($sfForm->getFormFieldSchema() as $field): ?> | |
<?php if(!$field->isHidden()): ?> | |
<?php if( in_array($field->getName(), $dateFields )): ?> | |
<?php $format =($field->hasError()) ? getErrorFieldFormat($field, 'date') : getSuccessFieldFormat($field, 'date'); ?> | |
<?php elseif(in_array($field->getName(),$selectFields)): ?> | |
<?php $format =($field->hasError()) ? getErrorFieldFormat($field, 'select'): getSuccessFieldFormat($field, 'select'); ?> | |
<?php elseif( in_array($field->getName(), $textareaFields)): ?> | |
<?php $format =($field->hasError()) ? getErrorFieldFormat($field, 'textarea') : getSuccessFieldFormat($field, 'textarea'); ?> | |
<?php elseif( in_array($field->getName(), $fileFields)): ?> | |
<?php $format =($field->hasError()) ? getErrorFieldFormat($field, 'file') : getSuccessFieldFormat($field, 'file'); ?> | |
<?php else: ?> | |
<?php $format =($field->hasError()) ? getErrorFieldFormat($field,'text',$extraElements): getSuccessFieldFormat($field,'text',$extraElements); ?> | |
<?php endif; ?> | |
<?php $field->getWidget()->getParent()->getFormFormatter()->setRowFormat($format); ?> | |
<?php echo $field->renderRow(); ?> | |
<?php else: ?> | |
<?php echo $field; /* render hidden field */ ?> | |
<?php endif; ?> | |
<?php endforeach; ?> | |
<?php else: /* Show form fields with the Boostrap default styles */ ?> | |
<?php foreach($sfForm->getFormFieldSchema() as $field): ?> | |
<?php if(!$field->isHidden()): ?> | |
<?php if( in_array($field->getName(), $dateFields)): ?> | |
<?php $format = getFieldFormat($field, 'date'); ?> | |
<?php elseif( in_array($field->getName(), $selectFields)): ?> | |
<?php $format = getFieldFormat($field, 'select'); ?> | |
<?php elseif( in_array($field->getName(), $textareaFields)): ?> | |
<?php $format = getFieldFormat($field, 'textarea'); ?> | |
<?php elseif( in_array($field->getName(), $fileFields)): ?> | |
<?php $format = getFieldFormat($field, 'file'); ?> | |
<?php else: ?> | |
<?php $format= getFieldFormat($field, 'text', $extraElements); ?> | |
<?php endif; ?> | |
<?php $field->getWidget()->getParent()->getFormFormatter()->setRowFormat($format); ?> | |
<?php echo $field->renderRow(); ?> | |
<?php else: ?> | |
<?php echo $field; /*render hidden field */ ?> | |
<?php endif; ?> | |
<?php endforeach; ?> | |
<?php endif;?> | |
<?php | |
} | |
function getFieldFormat($sfFormField, $type='text', $extra = array()) | |
{ | |
switch($type) | |
{ | |
case 'date': | |
return '<div class="control-group"> | |
<label for="" class="control-label">%label%</label> | |
<div class="controls"> | |
<div id="'. $sfFormField->getName() .'" class="input-append date" data-date="' . date('m/d/Y',strtotime($sfFormField->getValue())) . '" data-date-format="mm/dd/yyyy"> | |
<input name="'. $sfFormField->getName() .'" class="span2" size="16" type="text" value="' . date('m/d/Y',strtotime($sfFormField->getValue())) . '"></input> | |
<span class="add-on"><i class="icon-th"></i></span> | |
</div> | |
</div> | |
</div>'; | |
case 'select': | |
case 'textarea': | |
return '<div class="control-group"> | |
<label for="" class="control-label">%label%</label> | |
<div class="controls"> | |
%field% | |
</div> | |
</div>'; | |
case 'file': | |
return '<div class="control-group"> | |
<label for="" class="control-label">%label%</label> | |
<div class="controls"> | |
<input type="file" id="'. $sfFormField->getName() .'" name="'. $sfFormField->getName() .'" class="input-append file"></input> | |
</div> | |
</div>'; | |
default: | |
$fldname = $sfFormField->getName(); | |
@$extraElement=(array_key_exists($fldname, $extra)) ? $extra[$fldname] : ''; | |
return '<div class="control-group"> | |
<label for="" class="control-label">%label%</label> | |
<div class="controls"> | |
%field%'.@$extraElement.' | |
</div> | |
</div>'; | |
} | |
} | |
function getErrorFieldFormat($sfFormField, $type='text', $extra=array()) | |
{ | |
switch($type) | |
{ | |
case 'date': | |
return '<div class="control-group error"> | |
<label for="inputError" class="control-label">%label%</label> | |
<div class="controls"> | |
<div id="'. $sfFormField->getName() .'" class="input-append date" data-date="' . date('m/d/Y',strtotime($sfFormField->getValue())) . '" data-date-format="mm/dd/yyyy"> | |
<input name="'. $sfFormField->getName() .'" class="span2" size="16" type="text" value="' . date('m/d/Y',strtotime($sfFormField->getValue())) . '"></input> | |
<span class="add-on"><i class="icon-th"></i></span> | |
<span class="help-inline">%error%%help%</span> | |
</div> | |
</div> | |
</div>'; | |
case 'select': | |
case 'textarea': | |
return '<div class="control-group error"> | |
<label for="inputError" class="control-label">%label%</label> | |
<div class="controls"> | |
%field% | |
<span class="help-inline">%error%%help%</span> | |
</div> | |
</div>'; | |
case 'file': | |
return '<div class="control-group error"> | |
<label for="inputError" class="control-label">%label%</label> | |
<div class="controls"> | |
<input type="file" id="'. $sfFormField->getName() .'" name="'. $sfFormField->getName() .'" class="input-append file"></input> | |
<span class="help-inline">%error%%help%</span> | |
</div> | |
</div>'; | |
default: | |
$fldname = $sfFormField->getName(); | |
@$extraElement=(array_key_exists($fldname, $extra)) ? $extra[$fldname] : ''; | |
return '<div class="control-group error"> | |
<label for="inputError" class="control-label">%label%</label> | |
<div class="controls"> | |
<input name="'. $sfFormField->getName() .'" type="text" value="' | |
. $sfFormField->getValue() | |
. '" name="' | |
. $sfFormField->getValue() | |
. '" id="' | |
. $sfFormField->getName() | |
. '"></input><span class="help-inline">%error%%help%</span>'.@$extraElement.' | |
</div> | |
</div>'; | |
} | |
} | |
function getSuccessFieldFormat($sfFormField, $type='text', $extra=array()) | |
{ | |
switch($type) | |
{ | |
case 'date': | |
return '<div class="control-group success"> | |
<label for="inputSuccess" class="control-label">%label%</label> | |
<div class="controls"> | |
<div id="' . $sfFormField->getName() . '" class="input-append date" data-date="' . date('m/d/Y',strtotime($sfFormField->getValue())) . '" data-date-format="mm/dd/yyyy"> | |
<input name="'. $sfFormField->getName() .'" class="span2" size="16" type="text" value="' . date('m/d/Y', strtotime($sfFormField->getValue())) . '"></input> | |
<span class="add-on"><i class="icon-th"></i></span> | |
<span class="help-inline">%error%%help%</span> | |
</div> | |
</div> | |
</div>'; | |
case 'select': | |
case 'textarea': | |
return '<div class="control-group success"> | |
<label for="inputSuccess" class="control-label">%label%</label> | |
<div class="controls"> | |
%field%'.@$extraElement.' | |
</div> | |
</div>'; | |
default: | |
$fldname = $sfFormField->getName(); | |
@$extraElement=(array_key_exists($fldname, $extra)) ? $extra[$fldname] : ''; | |
return '<div class="control-group success"> | |
<label for="inputSuccess" class="control-label">%label%</label> | |
<div class="controls"> | |
<input name="'. $sfFormField->getName() .'" type="text" value="' | |
. $sfFormField->getValue() | |
. '" name="' | |
. $sfFormField->getValue() | |
. '" id="' | |
. $sfFormField->getName() | |
. '"></input><span class="help-inline">%error%%help%</span>'.@$extraElement.' | |
</div> | |
</div>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment