Internationalize your Propel Forms
It has never been so easy to internationalize your Propel forms. In this post, you will learn how to leverage the new form framework bundled with symfony 1.1 to develop an interface to edit articles in several languages.
Let's take a very simple internationalized Propel schema:
propel:
article:
id: ~
author: varchar(255)
created_at: ~
article_i18n:
title: { type: varchar, size: 255, required: true }
content: longvarchar
After your database has been configured, use the propel:build-all
task to generate the Propel model and form classes:
$ php symfony propel:build-all
The following files has been created automatically under your project root directory:
lib/
form/
ArticleForm.class.php
ArticleI18nForm.class.php
BaseFormPropel.class.php
model/
Article.php
ArticlePeer.php
ArticleI18n.php
ArticleI18nPeer.php
We want to be able to update both the English and French versions of an article in the same interface:
If you've already tried to code this kind of interface with symfony 1.0, you know that's a long and a somewhat annoying task.
Thanks to the symfony 1.1 enhancements, it will take us 2 minutes to make it work.
First, configure the languages you want to handle in the ArticleForm
class:
class ArticleForm extends BaseArticleForm { public function configure() { $this->embedI18n(array('en', 'fr')); $this->widgetSchema->setLabel('en', 'English'); $this->widgetSchema->setLabel('fr', 'French'); } }
Secondly, generate a CRUD module to provide a web interface to be able to list, create, update, and delete articles:
$ php symfony generate:crud frontend article Article
Enjoy the fully working module by browsing to /frontend_dev.php/article
. If you try to submit the edit form without any title, you will see that the database constraints defined in the schema are also automatically enforced.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Just a question, how can we specialize a forms regarding the culture of the current loggued user?
Anyway, you definitely rock, dudes.
thank you for the valuable information.
Giles, you're talking about i10n and i18n relationship, but it's not handled currently by the new forms framework.
However, you could easily write your own widgets/validators to handle this (which would also be very great contribution to the sfFormExtraPlugin by the way;-))
php symfony propel:generate-crud backend page Page
error:
No connection params set for propel
can you advice?