Blue Horn

Symfony Framework

Symfony 1.2 sfForm formatter to add stars on required fields

August 31, 2009 by Sid in Symfony Framework with 12 Comments

The following is a form formatter class I developed to automatically add stars * on required fields. Feel free to use it and let me know if it can be improved.

You might ask, why not just use setLabels?

Firstly, if you already set required fields to true/false in setValidators, why do you have to add the stars manually again in setLabels? It just doesn’t make sense!

Secondly, using setLabels to add stars is interfering with language translation.

Thirdly, this is I believe a proper (or proper-ish?) way to do this kind of things with sfForm.

OK, let’s get into it!

Below is an example on how to use it in your form class

setWidgets(array(
      'first_name'           => new sfWidgetFormInput(array(), array('maxlength' => 50)),
      'last_name'            => new sfWidgetFormInput(array(), array('maxlength' => 50)),
      'email'                => new sfWidgetFormInput(array(), array('maxlength' => 128)),
    ));

    $this->setValidators(array(
      'first_name'           => new sfValidatorString(array('max_length' => 50, 'required' => true)),
      'last_name'            => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'email'                => new sfValidatorEmail(array('max_length' => 128, 'required' => true)),
    ));

    $decorator = new sidFormFormatter($this->widgetSchema, $this->validatorSchema);
    $this->widgetSchema->addFormFormatter('custom', $decorator);
    $this->widgetSchema->setFormFormatterName('custom');    
  }
}

This is optional but you might want to add the following into your css (e.g.: main.css)

em.required {font-size:1.2em;font-weight:bold;margin:0px;padding:0px;}
label.required {font-weight:bold;}

Last but most importantly, save this formatter class in your project lib folder as sidFormFormatter.php:

\n
%label%
\n
%field%\n%help%
\n%hidden_fields%", $helpFormat = '
%help%
', $errorRowFormat = "
\n%errors%
\n", $errorListFormatInARow = "
    %errors%
\n", $errorRowFormatInARow = "
  • ↓ %error% ↓
  • \n", $namedErrorRowFormatInARow = "
  • ↓ %error% ↓
  • \n", $decoratorFormat = "%content%"; /** * @var sfValidatorSchema */ protected $validatorSchema = null; /** * @var array */ protected $params = array(); /** * Constructor * * Params: * - "required_label_class_name" css class name for label tag when the field is required field, the default is 'required'. * - "required_label_format" default is '%label% *'. * * @param sfWidgetFormSchema $widgetSchema * @param sfValidatorSchema $validatorSchema * @param array $params */ public function __construct(sfWidgetFormSchema $widgetSchema, sfValidatorSchema $validatorSchema, $params = array()) { $this->validatorSchema = $validatorSchema; $this->params = $params; parent::__construct($widgetSchema); } /** * Returns parameter identified with $name or if does not exist, returns $default. * * @param string $name * @param mixed $default * @return mixed */ public function getParameter($name, $default=null) { if (!isset($this->params[$name])) { return $default; } return $this->params[$name]; } /** * Generates a label for the given field name. * * @param string $name The field name * @param array $attributes Optional html attributes for the label tag * * @return string The label tag */ public function generateLabel($name, $attributes = array()) { $is_required = false; if ( $this->validatorSchema and isset($this->validatorSchema[$name]) ) { $validator = $this->validatorSchema[$name]; /* @var $validator sfValidatorBase */ if ( $validator->getOption('required') ) { $class_name = $this->getParameter('required_label_class_name', 'required'); if (isset($attributes['class'])) $attributes['class'] .= ' '.$class_name; else $attributes['class'] = $class_name; $is_required = true; } } $s = parent::generateLabel($name, $attributes); if ($is_required) { $format = $this->getParameter('required_label_format', '%label% *'); $s = str_replace('%label%', $s, $format); } return $s; } }

    That’s it! Don’t forget to “symfony cc” (clear cache).

    Happy coding!

    Tagged , ,

    Related Posts

    12 Comments

    1. beleneglorionAugust 31, 2009 at 1:31 am

      Nice code, perhaps could you make the “required” class configurable by passing options to the constructor, and do not replace $attributes[‘class’] by “required” concaten it, make the “*” configurable too

    2. SidAugust 31, 2009 at 2:04 amAuthor

      Hi, thank you for the feedback. I’ve updated the code.

    3. HugoAugust 31, 2009 at 4:49 am

      Thanks for sharing this. Your getParameter() method can be shorted as below to gain in readability :

      public function getParameter($name, $default=null)
      {
      if (!isset($this->params[$name]))
      {
      return $default;
      }

      return $this->params[$name];
      }

      ++

    4. SidAugust 31, 2009 at 10:06 amAuthor

      Thank you for the feedback, code is updated :)

    5. TimSeptember 1, 2009 at 10:32 pm

      Hi,

      Nice code! I try it, it works but I need update your constructor by updating sfWidgetSchema in sfWidgetFormSchema.

      Hope it helps

    6. Bob SagetSeptember 7, 2009 at 1:34 pm

      I agree with TIM. I also struggled a bit with the code but after changing the sfWidgetSchema tosfWidgetFormSchema in the Constructor, everything worked perfectly and oh so elegantly ! :)

      Thanks for the code !

    7. SidSeptember 7, 2009 at 1:44 pmAuthor

      Thanks for the feedback. I have updated the code to use sfWidgetFormSchema instead of sfWidgetSchema.

    8. AntoineNovember 16, 2009 at 3:14 pm

      Well done !

    9. SidJanuary 19, 2010 at 12:06 amAuthor

      @Antoine

      Thanks :)

    10. GarethFebruary 24, 2010 at 5:13 pm

      Just wondering if there was some way in the Form class to just add specific row formatting for 1 or 2 elements as this is unique to just this form, instead of having to create an entire decorator class to handle it.

    11. voiture belgiqueMay 10, 2011 at 2:35 am

      Hi ! nice snippet ! thank you very much !

    12. DiegoNovember 26, 2011 at 1:44 am

      tks for the snippet!! from Argentina

    Leave a reply

    Your email address will not be published. Required fields are marked *

    *

    fourteen − four =

    My Projects
    Restaurant Websites
    Websites