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. 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'=>'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. 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?
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.
精彩评论