views:

26

answers:

1

Hi!

Is there any possibility to use the gettext functionallity within the CakePHP model validation array?

Usually a programmer would do it like this:

class Data extends AppModel
{
 var $validate = array(
  'title' => array(
   'NichtLeer' => array(
    'rule' => array('between', 4, 20),
    'allowEmpty' => false,
    'message' => _('Bitte geben Sie einen Titel an!')
   )
  )
 );
}

But since it is not possible to use functions outside a method's scope, I have to find another clean alternative.

So, is there any alternative to the one, that defines the validations improvised in the model's setup method?

Regards, Benedikt

+1  A: 

Building the validate array in the constructor could be considered a clean alternative:

class Data extends AppModel {
    public function __construct() {
        $this->validate = array(
            'title' => array(
                'NichtLeer' => array(
                    'rule' => array('between', 4, 20),
                    'allowEmpty' => false,
                    'message' => _('Bitte geben Sie einen Titel an!')
                )
            )
        );
    }
}
Mike
Also, in case you may be interested, CakePHP comes with some goodies for [internationalization and localization](http://book.cakephp.org/view/1228/Internationalization-Localization).
Mike
Yes, I already use all this features :-) Thanks. I think I'll stick to this.
Benedikt R.
I only mentioned that because you used the single underscore function in your code, not the double underscore function that comes with CakePHP.
Mike
I am using the same approach as Mike's, but instead in __construct() I am setting this array in the beforeValidate(). For me it's more meaningful, but also in __construct() should work pretty much the same.
Nik