开发者

Using gettext in CakePHP model validation

开发者 https://www.devze.com 2023-01-10 09:14 出处:网络
Is there any possibility to use the gettext functionallity within the CakePHP model validation array?

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 improvise开发者_开发知识库d in the model's setup method?

Regards, Benedikt


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!')
                )
            )
        );
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号