【问题标题】:Using gettext in CakePHP model validation在 CakePHP 模型验证中使用 gettext
【发布时间】:2010-08-06 21:35:52
【问题描述】:

是否有可能在 CakePHP 模型验证数组中使用 gettext 功能?

通常程序员会这样做:

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

但是由于不可能使用方法范围之外的函数,我必须找到另一个干净的替代方案。

那么,是否有任何替代方法可以定义模型设置中的即兴验证方法?

问候, 本尼迪克特

【问题讨论】:

    标签: php validation cakephp methods gettext


    【解决方案1】:

    在构造函数中构建validate 数组可以被认为是一个干净的替代方案:

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

    【讨论】:

    • 另外,如果您可能感兴趣,CakePHP 为internationalization and localization 提供了一些好东西。
    • 是的,我已经使用了所有这些功能 :-) 谢谢。我想我会坚持下去。
    • 我只提到了因为你在代码中使用了单下划线函数,而不是CakePHP自带的双下划线函数。
    • 我使用与 Mike 相同的方法,但在 __construct() 中,我在 beforeValidate() 中设置了这个数组。对我来说它更有意义,但在 __construct() 中的工作方式也应该差不多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    相关资源
    最近更新 更多