English spoken conference

Symfony 5: The Fast Track

A new book to learn about developing modern Symfony 5 applications.

Support this project

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.

Comments

Great, this has been published just in time, actually me and my project thank you :p

Just a question, how can we specialize a forms regarding the culture of the current loggued user?

Anyway, you definitely rock, dudes.
Is there any capacity within this system to handle different form validation sets per language? For example, a COUNTY field in the UK would equate to STATE in the US - but the US one should be mandatory, whilst the UK field should not.
you have a very nice community ,
thank you for the valuable information.
Paul, the current culture can be retrieved using sfContext::getInstance()->getUser()->getCulture().

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;-))
But what if i just need the error messages like "invalid" in other language and my schema is not propel schema is not internationalized?? to be clear i have a normal form, 3 field = id, name, email, and if my email value is not a valid email string, then a message with an "invalid" string is generated. is there a way to automatically set this value depending the user culture, if the form is rendered with echo $form?... or I need to use in the form _('invalid') or something like that?
But what if i just need the error messages like "invalid" in other language and my schema is not propel schema is not internationalized?? to be clear i have a normal form, 3 field = id, name, email, and if my email value is not a valid email string, then a message with an "invalid" string is generated. is there a way to automatically set this value depending the user culture, if the form is rendered with echo $form?... or I need to use in the form _('invalid') or something like that?
leonardo> All labels, error messages and helps are internationalized if you enabled i18n in your factories.yml, just reference these strings in your XLIFF translation files.
i get this error when:
php symfony propel:generate-crud backend page Page

error:
No connection params set for propel
I also get next error "No connection params set for propel" when I tried to do "propel:generate-crud" but without embedI18n all works ok.

can you advice?

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.