01.21.08

Symfony 1.1 Forms - The Evolution of Functionality

Posted by ryan in symfony


Note: All the articles are now up to date as of this article.

This article is a follow up to the series "7 Days of Symfony Forms". You can find the first article of that series here: http://www.thatsquality.com/symfony/7-days-of-symfony-1-1-forms-widgets-and-validators-day1

Keeping up with Symfony

The new Form framework is now about a month old, and even though it is still quite young, it's already changed dramatically. As a consequence, MANY parts of my previous tutorials will need to be corrected - a task I will be working on right away. In general, it's essentially the same, and I was surprised how easy it was to update my code.

While watching my code quickly become obsolete makes me want to pull out my hair, there are several reasons I've decided to keep on top of the changes so closely

  • I'm developing software on the new framework, so I don't really have a choice
  • The evolution forces me to get familiar with the latest changes
  • Mistakes and points of frustration are now melting away as the community identifies and fixes them

The Changes

New function to check method

Here's a nice little one to start, and doesn't really have anything to do with the form system. I'm putting it here because it was used in my code, and appeared wherever forms go:

/* if ($this->getRequest()->getMethod()==sfRequest::POST) */ // this is deprecated and replaced with the following line
if ($this->getRequest()->isMethod('post'))

Shorter way to set widget and validator schema

Next up is a change that I really love. Even better, since this is something that's primarily generated automatically by the Propel Forms, I've had to change it in very few spots:

/* Original method to set your widget schema */
$this->setWidgetSchema(new sfWidgetFormSchema(array(
 /* the widgets */
)));

/* and the new, much shorter method */
$this->setWidgets(array(
 /* the widgets */
));

Thankfully, the same thing goes for validators

/* Original method to set your validator schema */
$this->setValidatorSchema(new sfValidatorSchema(array(
 /* the validators */
)));

/* and the new, much shorter method */
$this->setValidators(array(
 /* the validators */
));

Cloning of Embed Objects

This one is very important, but not something you might have run into. I actually did run into in part to of my recent article on template configuration. When I saw it, it surprised me. So, it was no surprise when I saw that Symfony had fixed it.

The basic idea before is that widgets, validators and forms were being added to respective schema by reference. Widgets were being added to widget schema, validators to validator schema, and forms embedding into forms with the actual object. The problem was that, if your form modified your embedded form, the original embedded form was in turn modified. Here's the idea below:

	$main_form = new sfForm();
	$embed_form = new myObjectForm();
	$main_form->embedForm('embed_form', $embed_form);

The last method, embedForm would modify the naming format of the embed_form so that its naming convention fits neatly inside main_form. The problem was that, if you tried to use the embed_form later, you found out that its naming schema had also changed. By embedding embed_form into main_form, our original embed_form was modified.

The solution was a series of changes where objects are now cloned. I haven't tested this yet, but I'm assuming that we can now safely use our forms in a decoupled fashion - without worrying about certain objects getting modified against our wishes.

Validator Renaming

This one is simple. sfValidatorAny became sfValidatorOr and sfValidatorAll became sfValidatorAnd. This was a good change - the names are much less confusing.

Arguments option on sfValidatorCallback

Another nice change - you can now specify an arguments option when using the sfValidatorCallback. These arguments will be passed to your callback function.

embedForm no longer takes name format arg

Before, when embedding a form, you could set the naming format you wanted for that embedded form. For whatever reason, this was removed. For the most part, I don't really think any of this. The beauty of embedding your forms is having your main form handle everything - no need to get dirty by overriding the naming format.

sfValidatorFile added

Yay! This was a huge whole when I started my 7 days of Symfony 1.1 Forms. It has now been filled, and I couldn't be happier. Not only was a validator added, but the new "method" for handling file uploads in Symfony with the new form system was unveiled. If you're interested, see the trac changelog for rev #6748. or see my article in day 5 of my 7 days of Symfony 1.1 Forms series, as file uploading is handled there.

sfFormField split into sfFormField and sfFormFieldSchema

This was a fairly basic change, and I've yet to dive into it too far. I'm not sure exactly what Fabien was going for here yet, but I have high hopes. As far as existing code, nothing of mine changed because of this - but I do like the organization.

Conclusion

Well, not bad. Like I said, because of the Propel Form generation, the above changes broke my code much less than I expected. The Symfony Form system has come a long way, and continues to improve. There are countless other little fixes and enhancements going on behind the scenes as well. Stay tuned for new updates and changes.

Thanks for the shares!
  • StumbleUpon
  • Sphinn
  • del.icio.us
  • Facebook
  • TwitThis
  • Google
  • Reddit
  • Digg
  • MisterWong