A typical Developer Blog
by Gordon Franke
Icon

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.

Author:

Category: symfony, Uncategorized

Tagged: , , , ,

10 Responses

  1. NiKo says:

    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 (†)

  2. pacoweb says:

    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 !-)

  3. thanks. i have fix it.

  4. atanu says:

    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 .

  5. Sergio says:

    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.

  6. 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?

  7. Sergio says:

    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.

  8. Don Camillo says:

    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?

  9. Guto says:

    great job! thanks.

Leave a Reply

CommentLuv badge