Skip to content

The form.post_configure event

by Matthias Noback on July 27th, 2010

You may find yourself copying and pasting code in the configure method of several forms. For example, when you wish to change the standard format of a sfWidgetFormDate widget. for all the forms you have in your back-end application. Of course you can extend the widget class, and set the format option, but you can also use the symfony event form.post_configure to loop through all the form’s widgets and set options for them. In your application configuration class (you may find this class in /apps/backend/config/backendConfiguration.class.php, if your application is called “backend”) modify the code as follows:

class backendConfiguration extends sfApplicationConfiguration
{
  public function configure()
  {
    // connect to the form.post_configure event
    $this->dispatcher->connect('form.post_configure', array($this, 'listenToFormPostConfigure'));
  }

  /**
   * Listens to the form.post_configure event and dispatches the call to appFormMethods::postConfigure
   */
  static public function listenToFormPostConfigure(sfEvent $event)
  {
    $form = $event->getSubject();

    $callable = array('appFormMethods', 'postConfigure');
    call_user_func($callable, $form);
  }
}

Then, create a file, for example in /lib/event/, named appFormMethods.class.php with an abstract class appFormMethods and one static public method postConfigure. This is an example of the kind of things you may want to do in the appFormMethods::postConfigure method:

abstract class appFormMethods
{
  static public function postConfigure(sfForm $form)
  {
    // define default options for date widgets
    $date_format = '%day%/%month%/%year%';
    $date_empty_values = array('day' => 'day', 'month' => 'month', 'year' => 'year');

    // define default options for time widgets
    $time_format = '%hour%:%minute%';
    $time_empty_values = array('hour' => 'hours', 'minute' => 'minutes');

    // loop through all the form fields
    foreach ($form as $name => $form_field)
    {
      /* @var $form_field sfFormField */
      $widget = $form_field->getWidget();

      // set default format for each date/time widget
      switch (get_class($widget))
      {
        case 'sfWidgetFormDate':
          $widget->setOption('format', $date_format);
          $widget->setOption('empty_values', $date_empty_values);
          break;
        case 'sfWidgetFormTime':
          $widget->setOption('format', $time_format);
          $widget->setOption('empty_values', $time_empty_values);
          break;
        case 'sfWidgetFormDateTime':
          $widget->setOption('date', array('format' => $date_format, 'empty_values' => $date_empty_values));
          $widget->setOption('time', array('format' => $time_format, 'empty_values' => $time_empty_values));
          break;
        case 'sfWidgetFormInputText':
          if (!$widget->getAttribute('size'))
          {
            $widget->setAttribute('size', 45);
          }
          break;
      }
    }
  }
}

We loop through all the form’s fields and do specific things based on the type of widget that is involved. This may also be the place to globally change options for the form’s validators. The nice thing is: this way it is possible to change options of widgets and validators after all setup and configure methods of a form have been called, so by using the form.post_configure event you can override widget/validator settings for every form in the application.

4 Comments
  1. Sander Coolen permalink

    I don’t see how appFormMethods is an abstract class. Yes, there are only static methods, but it’s not abstract. A class is abstract when only its subclasses can be instantiated and it has one or more abstract methods.

  2. Jean-Marie de Boer permalink

    Nice, but I would see that fact that this is done after the setup and configure steps as a drawback. It means that as soon as I want to change one type of widget in this way, it is no longer possible to have one that is different from the rest.

    Of course, if you want to make all widgets of a certain type appear the same that will work, but then you are kind of stuck with it…

  3. kris permalink

    Looks good,
    I’d like to do the same thing in a custom admin generator theme. But I did not find the way to doi it.
    Any advice,will be appreciate.
    Thanks

  4. miky permalink

    Great work.

    @kris: Did you successed to customize the admin generator ?
    I would like to do something similar.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS