简体   繁体   English

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

[英]How to validate email using Kohana 3.1 ORM

I'm using Kohana 3.1 framework to do a simple validation using Kohana's ORM and Validation built in classes. 我正在使用Kohana 3.1框架使用Kohana的ORM和内置在类中的Validation进行简单的验证。 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 ... catch构造验证并保存对象:

try
{
    $t = array(
        'first_name'=>'pippo',
        'email'=>'foo@foo.com',
    );

    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. 现在,上面的$t数组应该可以验证,但不能验证。 It instead throws an exception and dies calling Debug::vars and printing this error: 相反,它将引发异常并死于调用Debug::vars并显示此错误:

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

This is clearly not true, what I'm doing wrong? 这显然是不正确的, 我在做什么错?

So have you sorted it or not? 那么,您是否对它进行了排序?

instead of: 代替:

$t = array(
  'first_name'=>'pippo',
  'email'=>'foo@foo.com',
);

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

why don't you try: 你为什么不尝试:

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

$customer->first_name = 'pippo';
$customer->email = 'foo@foo.com';

$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. 只是一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM