如何使用 Kohana 3.1 ORM 验证电子邮件
我正在使用 Kohana 3.1 框架使用 Kohana 的 ORM
和 Validation
内置类进行简单的验证。让我们看看代码...
在模型中,我有这些简单的规则:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么你整理了吗?
而不是:
你为什么不尝试一下:
它更加清晰和明确。这样您就不会再对电子邮件是否已设置有任何困惑,因此您知道开始在其他地方查找。
只是一个想法。
So have you sorted it or not?
instead of:
why don't you try:
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.