First, Marijn & Nicolas, thanks for sharing your time and code.
Following Nicolas' implementation I used a new Formatter to be able to
apply a CSS rule on the label:
class sfWidgetFormSchemaFormatterTable2 extends
sfWidgetFormSchemaFormatter
{
protected
$rowFormat = "<tr%required%>\n <th>%label%</th>\n <td>
%error%%field%%help%%hidden_fields%</td>\n</tr>\n",
$errorRowFormat = "<tr><td colspan=\"2\">\n%errors%</td></tr>\n",
$helpFormat = '<br />%help%',
$decoratorFormat = "<table>\n %content%</table>";
public function formatRow($label, $field, $errors = array(), $help =
'', $hiddenFields = null)
{
return strtr($this->getRowFormat(), array(
'%required%' => strstr($field, 'required') ? '
class="required"' : '',
'%label%' => $label,
'%field%' => $field,
'%error%' => $this->formatErrorsForRow($errors),
'%help%' => $this->formatHelp($help),
'%hidden_fields%' => is_null($hiddenFields) ? '%hidden_fields
%' : $hiddenFields,
));
}
}
Is it too hackish for symfony standard ? :)
Ideally I would like to have the required class to be set at the
parent node of label and input|select in order to use those CSS rules:
form .required label {font-weight:bold;}
form .required label:after {content:"*";}
form label {font-weight:normal;}
which should work for list , table and div Formatter.
Also in handleRequiredFields() how could we loop through pre and post
validator ?
For example: sfGuardUserForm set the option required for the field
password on the postValidator()
Regards,
Marc Carlucci
On Aug 22, 10:02 am, "Nicolas Perriault" <[EMAIL PROTECTED]> wrote:
> 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
> Perriaulthttp://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
-~----------~----~----~----~------~----~------~--~---