<?php

/**
 *@author Victor Zamfir <zamfir.victor@gmail.com> 
 */
class vzFormFormatter extends sfWidgetFormSchemaFormatterList
{
	protected
    $rowFormat       = "<li>\n <div class=\"field-info\">%label% %help%</div> <div class=\"form-field\">%field%</div> \n %error% \n%hidden_fields%</li>\n",
    $errorRowFormat  = "<li>\n%errors%</li>\n",
    $helpFormat      = '<a class="help-text" title="%help%">?</a>',
    $decoratorFormat = "<ul class=\"fields\">\n  %content%</ul>",
		$requiredLabelClass = 'required';
		
  public function generateLabel($name, $attributes = array())
  {
		if ($this->fieldIsRequired($name)) {
			$attributes['class'] = isset($attributes['class']) ? $attributes['class'].' '.$this->requiredLabelClass : $this->requiredLabelClass;
		}
    return parent::generateLabel($name, $attributes);
  }
	
	public function fieldIsRequired($name)
	{
		$widget = $this->widgetSchema;
    do
    {
      $requiredFields = (array) $widget->getOption('required_fields');
    }
    while ($widget = $widget->getParent());
 		return in_array($this->widgetSchema->generateName($name), $requiredFields);
	}
	
	public function generateLabelName($name)
	{
		return parent::generateLabelName($name) . ($this->fieldIsRequired($name) ? $this->widgetSchema->renderContentTag('sup', '*') : '');
	}
 
}
