Mar 19, 2009
How can i mark all required form fields with a *?
create file lib/gfFormHerlp.class.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?php /** * Some usefull form helper * * @package symfony * @subpackage helper * @author Gordon Franke <gfranke@nevalon.de> * @link http://www.nevalon.de */ class gfFormHelper { /** * Add * to required field labels * * @param sfForm $form * @param string $symbol * @param string $title * * @return void */ public static function addRequiredToLabel(sfForm $form, $symbol = '*', $title = 'This field is mandatory.') { $widgetSchema = $form->getWidgetSchema(); $validatorSchema = $form->getValidatorSchema(); foreach($form->getFormFieldSchema()->getWidget()->getFields() as $key => $object) { $label = $form->getFormFieldSchema()->offsetGet($key)->renderLabelName(); if(isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('required') == true) { $label .= '<sup title="' . $widgetSchema->getFormFormatter()->translate($title) . '">' . $symbol . '</sup>'; } $widgetSchema->setLabel($key, $label); } } } |
add at the end of your form setup or better when you have subforms into the configure method this line
1 | gfFormHelper::addRequiredToLabel($this); |
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.











You can also extends the sfForm and override its configure() method to implement the same behavior, but beware that the Doctrine|Propel based forms only inherit from sfForm by default
NiKo’s last blog post..Non, je ne suis pas mort (†)
Thanks for this neat little piece of code.
Just a typo though on line 27 :
+ $label .= ‘getFormFormatter()->translate($title) . ‘”>’ . $symbol . ”;
I had a solution based on creating a new formFormatter, but it was so heavy ! I do tend to think the sfForm as a whole needs some diet at some point !-)
thanks. i have fix it.
I have using advanceAdminGenerator for creating admin. In this admin area i want to marked
all required form field with *. but i can’t impliments this… pls give me a suggestion .
Hi, thanks for the code, it’s very useful. Can I ask you for its license? Right now, I just would like to use it in an academic project (I copied the whole code, including @author comments).
Bye.
You can use it for free the only requirement is to copy also the @author and @link tag
Is the project a internal project or can you post a url?
It’s just a project to get my degree, and I’m using Symfony… so nice and hard at the same time for beginners like me xD. By the moment, no url, sorry.
I just noticed when you rename your labels by the methode setLabels i.e. this very helpful and nice Helper is not working anymore. Does anyone knows how to solve this behaviour?
Make sure that you call the helper function at the end of the form configure code
For symfony >= 1.3 see
http://blog.nevalon.de/en/wie-kann-man-in-symfony-1-3-einfach-alle-formular-pflichtfelder-kennzeichnen-how-can-i-simple-mark-all-required-form-fields-in-symfony-1-3-20091212
great job! thanks.