Skip to content

Instantly share code, notes, and snippets.

@kipit
Last active August 29, 2015 14:23
Show Gist options
  • Save kipit/1aff5830be78c4e93127 to your computer and use it in GitHub Desktop.
Save kipit/1aff5830be78c4e93127 to your computer and use it in GitHub Desktop.

This will format your Twitter Bootstrap forms nicely. Block errors, inline help.

<?php
require_once dirname(__FILE__).'/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
sfWidgetFormSchema::setDefaultFormFormatterName('bootstrap');
}
}
<?php
class sfWidgetFormSchemaFormatterBootstrap extends sfWidgetFormSchemaFormatter
{
protected
$rowFormat = "<div class=\"control-group %error_class%\">\n%label%\n<div class=\"controls\">%field%\n%help%\n%error%</div>\n%hidden_fields%</div>\n",
$helpFormat = '<span class="help-inline">%help%</span>',
$errorRowFormat = "\n%errors%\n",
$errorListFormatInARow = " \n%errors%\n",
$errorRowFormatInARow = " <div class=\"help-block\">%error%</div>\n",
$namedErrorRowFormatInARow = " <div class=\"help-block\">%name%: %error%</div>\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,
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment