Blog

Internationalize your Propel Forms

Symfony Live 2010 Paris Conference

« Back to the Blog

Categories

Feeds

feed Posts feed

comments feed Comments feed

symfony training
Be trained by symfony experts
Jul 22: Paris (1.2 + Doctrine - Français)
Aug 19: San Francisco (1.2 + Doctrine - English)
Sep 23: Paris (1.2 + Doctrine - Français)
Oct 21: Nantes (1.2 + Doctrine - Français)
Nov 18: Paris (1.2 + Doctrine - Français)
and more...

Archives

Creative Commons License This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.

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.

Comments comments feed

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.