I am using a new localization system on our site that requires all the displayed strings to be run through the SDK so all can be modified and localized on the go remotely. My only issue is that I am so far unable to run validation messages through this system as they are defined in the model. Is there any option how to do this for all, preferably in one go, without having to re-do the entire model system?
I am using following:
public $validate = array(
'name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Name of the application can not be empty',
),
),
);
and would like to achieve something like this (which obviously wouldn't work):
public $validate = array(
'name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => MyLocalizationMethod('Name of the application can not be empty'),
),
),
);
For this case dont set the messages in the $validate
. Rather define the __construct()
and inside the __construct()
set the messages. Try with -
$this->Model->validationErrors['name']['notEmpty'] = MyLocalizationMethod("time is less than 30");
CakePHP is already setup to easily allow for localisation of your validation error messages.
All you need to do is generate a POT file that can then be translated for each localisation:-
./Console/cake i18n extract
There are many free POT file editors out there. Personally, I use poedit.
You therefore shouldn't need to edit your models. The setup for localisation has already been done just be writing validation rules in your models.
Otherwise, I'd be inclined to use sgt Bose's answer, but set the entire Model::validate
array in the constructor; it will make the code more readable and easier to maintain.