Skip to content

Instantly share code, notes, and snippets.

@richsage
Created February 9, 2011 15:25
Show Gist options
  • Save richsage/818639 to your computer and use it in GitHub Desktop.
Save richsage/818639 to your computer and use it in GitHub Desktop.
RequiredLabelsFormatterTable.class.php
<?php
class RequiredLabelsFormatterTable extends sfWidgetFormSchemaFormatterTable
{
protected $requiredLabelClass = 'required';
/**
* Generates a label, but adds a '*' if the field is required
* @see sfWidgetFormSchemaFormatter::generateLabel()
*/
public function generateLabel($name, $attributes = array())
{
// loop up to find the "required_fields" option
$widget = $this->widgetSchema;
do
{
$requiredFields = (array) $widget->getOption('required_fields');
}
while ($widget = $widget->getParent());
// add a class (non-destructively) if the field is required
if (in_array($this->widgetSchema->generateName($name), $requiredFields))
{
$attributes['class'] = isset($attributes['class']) ?
$attributes['class'].' '.$this->requiredLabelClass :
$this->requiredLabelClass;
$labelName = $this->generateLabelName($name);
if (strlen($labelName))
{
$labelName .= ' ' . $this->widgetSchema->renderContentTag('span', '*', array('class' => 'compulsory'));
$this->widgetSchema->setLabel($name, $labelName);
}
}
return parent::generateLabel($name, $attributes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment