We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Howto customize/translate model messages

The problem with model validation messages in my application: 1) They need to be human-readable and may vary depending on module. 2) They need to be translated in several languages.

Here comes the solution. One may ask why don't override Model's getMessages method? The answer is it'll be less configurable and (critical for me) wouldn't affect ValidationFailed::getMessages() as I prefer to throw exceptions on save() failed.

Hope it will be useful for someone, other solutions are welcome ;-)



$di['messages'] = function () {
    $messages = require __DIR__ . '/messages/en.php';
    return new \Phalcon\Translate\Adapter\NativeArray(array(
        'content' => $messages,

$di['modelsManager'] = function () use ($di) {
    $eventsManager = new \Phalcon\Events\Manager();
    $eventsManager->attach('model', function ($event, $model) use ($di) {
        if ('onValidationFails' === $event->getType()) {
            if ($t = $di->get('messages')) {
                foreach ($model->getMessages() as $message) {
                        $t->_($message->getMessage(), $model->toArray()));
    $modelsManager = new \Phalcon\Mvc\Model\Manager();
    return $modelsManager;


class User extends \Phalcon\Mvc\Model
    public $login;
    const E_EXISTS = 'User exists';

    public function validation()
        $this->validate(new Phalcon\Mvc\Model\Validator\Uniqueness(array(
            'field'   => 'login',
            'message' => self::E_EXISTS,
        return !$this->validationHasFailed();


return array(
    User::E_EXISTS => 'User %login% already exists',

From our side, we've created a dedicated flash service which does translation on last time :

class Direct extends \Phalcon\Flash\Direct
public function message($type, $translationKeyword)
        return parent::message($type, $this->di->get('translate')->_($translationKeyword));

Hey all, I am using Flash session, I have registered it in my service file and using $this->flash->error("message_token"); in every controller, also there is a service named translator which has been used all over the app to translate the tokens.

Now i want to translate my flash messages, is there anyway which I can use in service files, or any other suitable method for this task.

Can someone please guide me how can i do it?