On Fri, Aug 22, 2008 at 10:04 AM, Marijn <[EMAIL PROTECTED]> wrote:

> The function could extract the value from the validator on that field.
> For now I have implemented this in my own subclassed sfForm like this.
> Hopefully I'm not the only one who would like to see this implemented
> on sfFormField:-D

Here's my own implementation of a base form class:

<?php
/**
 * Symfonians base non-Propel form class
 *
 */
class BaseForm extends sfForm
{
  const REQUIRED_CLASS_NAME = 'required';

  /**
   * Public constructor
   *
   * @see sfForm
   */
  public function __construct($defaults = array(), $options = array(),
$CSRFSecret = null)
  {
    parent::__construct($defaults, $options, $CSRFSecret);

    $this->postSetup();
  }

  /**
   * Post setup final method, cannot be overriden
   *
   */
  final private function postSetup()
  {
    $this->handleRequiredFields();
  }

  /**
   * Adds a CSS class name to required widgets
   *
   */
  protected function handleRequiredFields()
  {
    if (!$this->validatorSchema)
    {
      return;
    }

    foreach ($this->validatorSchema->getFields() as $fieldName => $validator)
    {
      /* @var $validator sfValidatorBase */
      if (true === $validator->getOption('required'))
      {
        if (!array_key_exists($fieldName, $this->widgetSchema->getFields()))
        {
          continue;
        }

        /* @var $widget sfWidget */
        $widget = $this->widgetSchema[$fieldName];

        $class = trim($widget->getAttribute('class'));

        if (!$class)
        {
          $class = self::REQUIRED_CLASS_NAME;
        }
        else if (!preg_match(sprintf('/%s/i',
self::REQUIRED_CLASS_NAME), $class))
        {
          $class = sprintf('%s%s', $class, self::REQUIRED_CLASS_NAME);
        }

        $widget->setAttribute('class', $class);
      }
    }
  }
}

As you can see, all required widgets have a DOM class set
automatically when they are required by a validator. The main problem
is if the class attribute is added ok for a widget rendering, it's
quite difficult to add something for its label or the form row
container, because the renderRow() of the sfWidgetSchemaFormater class
has no reference to the validatorSchema... so a hackish way to achieve
such a detection would be to parse the rendered widget to see if it
contains the required DOM class, but it's way to hackish to exist in
symfony :-)

If anybody have a clever idea, it's more than welcome !

++

-- 
Nicolas Perriault
http://prendreuncafe.com - http://symfonians.net - http://sensiolabs.com
Phone: +33 660 92 08 67

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/symfony-devs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to