2011-07-24 34 views
1

我使用Kohana 3.1框架做一個簡單的驗證使用Kohana的ORMValidation內置類。讓我們來看看代碼...如何驗證電子郵件使用Kohana 3.1 ORM

模式我有這些簡單的規則:

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" 
) 

這顯然是不正確的,什麼,我做錯了什麼?

+0

我在關係方面做了錯誤,實際上字段'email'列在我還沒有鏈接的外部相關表上。 – gpasci

回答

1

那麼你有沒有排序呢?

代替:

$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(); 

它的一點點輪廓清晰和明確。然後你就不會對電子郵件是否設置有任何困惑,所以你知道開始尋找其他地方。 只是一個想法。