Posts Tagged ‘ validation ’

Model validation messages and i18n

Hi there!

I believe that everyone who already used i18n within CakePHP noticed that when you define model validation messages it simply doesn’t get translated, so.. what to do to bring i18n to work with these messages?

It’s simple and easy, there’s a method invalidate() in the Model class, it is used for marking fields as invalid, it receives a field to invalidate and a message, everything we need to do is overriding this method in our AppModel and calling the Model invalidate() method with our message wrapped by the translation function as shown below:

class AppModel extends Model {

	function invalidate($field, $value = true) {
		return parent::invalidate($field, __($value, true));
	}

}

.. and voilá! You won’t need to bother with it anymore :-)

Advertisements