Posts Tagged ‘validation’

Validation error message i18n

Thursday, July 23rd, 2009

In a Model, we can define validation error message as follow

<?php
class User extends AppModel {
	var $validate = array(
		'email' => array(
			"email_invalid" => array('rule' => VALID_EMAIL,
				'required' => true,
				'message'    =>    'Invalid Email address.',
	),

But, can not use i18n function __() for validation error messages .
This is a solution to do it.

<?php
class AppModel extends Model {

	//Validation message i18n
	function invalidate($field, $value = true){
		parent::invalidate($field, $value);
		$this->validationErrors[$field] = __($value, true);
	}
}

Model::invalidate method is called in validation processing(Model::validates , Model::invalidFields).
This solution override Model::invalidate and set __() in each error messsage.

Then, you write po file of each language, language of validation error messages change depending on browser language configuration.