A typical Developer Blog
by Gordon Franke
Icon

Wie kann ich alle Formular Pflichtfelder mit einem * kennzeichnen?

Erstelle die Datei 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);
    }
  }
}

Füge folgendes am ende deiner Formular in der setup oder besser wenn du Unterformulare hast in der configure Methode ein

1
gfFormHelper::addRequiredToLabel($this);

No related posts.

Ähnliche Artikel bereitgestellt von Yet Another Related Posts Plugin.

Author:

Category: symfony, Uncategorized

Tagged: , , , ,

10 Responses

  1. NiKo sagt:

    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 sagt:

    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. atanu sagt:

    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 .

  4. Sergio sagt:

    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.

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

  6. Sergio sagt:

    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.

  7. Don Camillo sagt:

    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?

Leave a Reply

CommentLuv badge