Kohana 3.1 国家/地区验证回调

发布于 2024-11-15 21:56:17 字数 738 浏览 3 评论 0原文

我正在尝试在我的网站上设置注册功能,我需要验证用户输入的选择类型国家/地区:

public function rules()
    {
        return array(
            ......
            'country' => array(
                array('not_empty'),
                array('digit'),
                array(array($this, 'country_from_list'), array(':validation', ':field'))
            ),
            ......
        );
    }

这是我的回调:

public static function country_from_list($values)
{
    // array id => value
    $countries = ORM::factory('country')->getActive('array');

    return Validation::factory($values)
        ->rule('country', 'in_array', array(':value', $countries));
}

但它不起作用。有什么想法吗?我正在尝试从 Kohana 3.0.9 移植它......

I'm trying to setup the registration function on my site and I need to validate user's input of country of select type:

public function rules()
    {
        return array(
            ......
            'country' => array(
                array('not_empty'),
                array('digit'),
                array(array($this, 'country_from_list'), array(':validation', ':field'))
            ),
            ......
        );
    }

and here is my callback:

public static function country_from_list($values)
{
    // array id => value
    $countries = ORM::factory('country')->getActive('array');

    return Validation::factory($values)
        ->rule('country', 'in_array', array(':value', $countries));
}

but it doesn't work. Any ideas? I'm trying to port it from Kohana 3.0.9...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瓜 2024-11-22 21:56:17

如果您将 Validation 对象传递给回调方法,则如果它不符合您的条件,您可以对其执行自定义错误。 (在回调内部:$validation->error(...))

否则,您的回调应该接受字段的值,返回 bool 并如下所示:

// The callback method is static, you shouldn't call it on the instance.
array(array('Model_Whatever::country_from_list'), array(':value'))

方法:

public static function country_from_list($value)
{
    return in_array($value, ORM::factory('country')->getActive('array'));
}

If you're passing the Validation object to the callback method, you can do a custom error on it if it fails your condition. (inside of your callback: $validation->error(...))

Otherwise, your callback should accept the field's value, return bool and look like this :

// The callback method is static, you shouldn't call it on the instance.
array(array('Model_Whatever::country_from_list'), array(':value'))

And the method:

public static function country_from_list($value)
{
    return in_array($value, ORM::factory('country')->getActive('array'));
}
深者入戏 2024-11-22 21:56:17

有一个问题(至少)。下面:

array(':value', $countries)

实际上是一个数组,其中:

  • 第一个元素是 :value 字符串,
  • 第二个元素是 ORM::factory('country')->getActive('array' 的结果)

但我不是 Kohana 3.1 验证方面的专家 - 我听说它自 3.0 以来已经发生了变化。

There is one issue (at least). The following:

array(':value', $countries)

is actually an array, where:

  • first element is :value string,
  • second element is the result of ORM::factory('country')->getActive('array'),

But I am not an expert in Kohana 3.1's validation - I heard it has changed since 3.0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文