如何使用 Kohana 3.1 ORM 验证电子邮件

发布于 2024-11-25 22:24:58 字数 1044 浏览 1 评论 0原文

我正在使用 Kohana 3.1 框架使用 Kohana 的 ORMValidation 内置类进行简单的验证。让我们看看代码...

模型中,我有这些简单的规则:

public function rules()
{
  return array(
    'first_name' => array(
      array('not_empty'),
    ),
    'email' => array(
      array('not_empty'),
      array('email'),
    ),
  );
}

然后在控制器中,我尝试验证并保存对象经典的 try ... catch 构造:

try
{
    $t = array(
        'first_name'=>'pippo',
        'email'=>'[email protected]',
    );

    ORM::factory('customer')->values($t)->save();

}
catch ( ORM_Validation_Exception $e )
{
    die(Debug::vars($e->errors('')));
}

现在上面的 $t 数组应该有效,但事实并非如此。相反,它会抛出异常并终止调用 Debug::vars 并打印此错误:

array(1) (
    "email" => string(23) "email must not be empty"
)

这显然不是真的,我做错了什么?

I'm using Kohana 3.1 framework to do a simple validation using Kohana's ORM and Validation built in classes. Let's see the code...

In the model I have these simple rules:

public function rules()
{
  return array(
    'first_name' => array(
      array('not_empty'),
    ),
    'email' => array(
      array('not_empty'),
      array('email'),
    ),
  );
}

then in the controller I try to validate and save the object with the classic try ... catch construct:

try
{
    $t = array(
        'first_name'=>'pippo',
        'email'=>'[email protected]',
    );

    ORM::factory('customer')->values($t)->save();

}
catch ( ORM_Validation_Exception $e )
{
    die(Debug::vars($e->errors('')));
}

Now the $t array above should validate, but it doesn't. It instead throws an exception and dies calling Debug::vars and printing this error:

array(1) (
    "email" => string(23) "email must not be empty"
)

This is clearly not true, what I'm doing wrong?

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

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

发布评论

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

评论(1

天赋异禀 2024-12-02 22:24:58

那么你整理了吗?

而不是:

$t = array(
  'first_name'=>'pippo',
  'email'=>'[email protected]',
);

ORM::factory('customer')->values($t)->save();

你为什么不尝试一下:

$customer = ORM::factory('customer');

$customer->first_name = 'pippo';
$customer->email = '[email protected]';

$customer->save();

它更加清晰和明确。这样您就不会再对电子邮件是否已设置有任何困惑,因此您知道开始在其他地方查找。
只是一个想法。

So have you sorted it or not?

instead of:

$t = array(
  'first_name'=>'pippo',
  'email'=>'[email protected]',
);

ORM::factory('customer')->values($t)->save();

why don't you try:

$customer = ORM::factory('customer');

$customer->first_name = 'pippo';
$customer->email = '[email protected]';

$customer->save();

Its a little bit more clean cut and explicit. Then you'd have never had any confusion on whether email was set, so you know to start looking else where.
Just a thought.

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