Skip to content

Instantly share code, notes, and snippets.

@pborreli
Created April 28, 2010 17:01
Show Gist options
  • Save pborreli/382376 to your computer and use it in GitHub Desktop.
Save pborreli/382376 to your computer and use it in GitHub Desktop.
adding ":" to all labels and "*" to all required fields inside symfony 1.3+ forms
<?php
class privateConfiguration extends sfApplicationConfiguration
{
public function configure()
{
$this->dispatcher->connect('form.post_configure', array($this, 'listenToFormPostConfigure'));
}
/**
* Listens to the view.configure_format event.
*
* @param sfEvent An sfEvent instance
* @static
*/
static function listenToFormPostConfigure(sfEvent $event)
{
$form = $event->getSubject();
$widgetSchema = $form->getWidgetSchema();
foreach ($form->getValidatorSchema()->getFields() as $fieldName => $validator)
{
if (isset($widgetSchema[$fieldName]))
{
$label = $widgetSchema[$fieldName]->getLabel() ? $widgetSchema[$fieldName]->getLabel() : sfInflector::humanize($fieldName);
$asterisk = $validator->getOption('required') ? '&nbsp;*' : null;
$widgetSchema[$fieldName]->setLabel($label . $asterisk . '&nbsp;:');
}
}
}
}
@jtexier
Copy link

jtexier commented Oct 18, 2010

Really neat method of solving the "required fields problem".
But it seems to not work with widget labels defined on a generator.yml file.
On an admin generated page, it looks like that labels are overriden after the form.post_configure event.
Note that the solution proprosed by Kriss works well in that case.

@pborreli
Copy link
Author

@jtexier indeed this solution is form based, can work while using it inside admin gen but with this limitation, personally i prefer define labels inside form and when it need to be overridden i do it inside generator.yml. so it's a feature ;)

@jtexier
Copy link

jtexier commented Oct 18, 2010

I agree with you on defining labels inside a class rather than on generator.yml file.
Unfortunatly, generator.yml is useful to globally define labels for list table columns, filter and form widgets.

Anyway, labels should be handled by only one class (the model one) and should be overriden by related form or filter classes if necessary...

@luads
Copy link

luads commented Jan 13, 2011

I've got duplicated asterisks on merged forms, so I've done this:

   if (substr($label, -1, 1) != '*') {
      $asterisk = $validator->getOption('required') ? '*' : null;
      $widgetSchema[$fieldName]->setLabel($label . $asterisk);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment