Tuesday, June 17, 2014

This will format your Twitter Bootstrap forms nicely. Block errors, inline help. (symfony 1.4 form formatter, decoration, twitter bootstrap, apply a formatter to an application)

123456789101112
 
require_once dirname(__FILE__).'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();
 
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
sfWidgetFormSchema::setDefaultFormFormatterName('bootstrap');
}
}
This will format your Twitter Bootstrap forms nicely. Block errors, inline help.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
 
class sfWidgetFormSchemaFormatterBootstrap extends sfWidgetFormSchemaFormatter
{
protected
$rowFormat = "
\"control-group %error_class%\">\n%label%\n
\"controls\">%field%\n%help%\n%error%
\n%hidden_fields%
\n",
$helpFormat = '%help%',
$errorRowFormat = "\n%errors%\n",
$errorListFormatInARow = " \n%errors%\n",
$errorRowFormatInARow = "
\"help-block\">%error%
\n",
$namedErrorRowFormatInARow = "
\"help-block\">%name%: %error%
\n",
$decoratorFormat = "%content%",
$widgetSchema = null,
$translationCatalogue = null;
public function generateLabel($name, $attributes = array()) {
$labelName = $this->generateLabelName($name);
if (false === $labelName)
{
return '';
}
if (!isset($attributes['for']))
{
$attributes['for'] = $this->widgetSchema->generateId($this->widgetSchema->generateName($name));
}
if (isset($attributes['class'])) {
$attributes['class'] .= ' ';
} else {
$attributes['class'] = '';
}
$attributes['class'] .= 'control-label';
return $this->widgetSchema->renderContentTag('label', $labelName, $attributes);
}
public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null)
{
return strtr($this->getRowFormat(), array(
'%label%' => $label,
'%error_class%' => count($errors) ? 'error' : '',
'%field%' => $field,
'%error%' => $this->formatErrorsForRow($errors),
'%help%' => $this->formatHelp($help),
'%hidden_fields%' => null === $hiddenFields ? '%hidden_fields%' : $hiddenFields,
));
}
}
PS: to use this format in backend only, you can do:
root/lib/form/BaseForm.class.php
class BaseForm extends sfFormSymfony
{

public function configure()
{

//use bootstrap form layout at backend
if (sfConfig::get('sf_app') == 'backend') sfWidgetFormSchema::setDefaultFormFormatterName('bootstrap');

No comments: