2011-03-25 55 views
1

使用Kohana 3.1與Auth ORM模塊,如果使用create_user創建新用戶,如何處理驗證異常併爲每個用戶顯示錯誤一個在頁面中?在這種情況下,密碼是短的(< 8個字符),但也可能是password_confirm不匹配password嘗試創建用戶時處理驗證異常(Kohana 3.1 ORM/Auth模塊)

$user = ORM::factory('user') 
     ->where('username', '=', 'admin')->find(); 
if(! $user->loaded()) 
{ 
    $this->template->content = __('Admin user does not exist. Creating...'); 

    $user = ORM::factory('user'); 
    $user->create_user(
     array(
     'username' => 'admin', 
     'email' => '[email protected]', 
     'password' => 'admin', 
     'password_confirm' => 'admin' 
     ), 
     array(
     'username', 
     'email', 
     'password' 
    )); 

    // remember to add the login role AND the admin role 
    // add a role; add() executes the query immediately 
    $user->add('roles', ORM::factory('role')->where('name', '=', 'login')->find()); 
    $user->add('roles', ORM::factory('role')->where('name', '=', 'admin')->find()); 
} 

回答

3

希望你已經找到了一個回答你的問題,但無論如何:

try 
{ 
    $user->create_user(array(..)) 
} 
catch (ORM_Validation_Exception $e) 
{ 
    $validation_errors = $e->errors(''); // an array of errors will be stored in $validation_errors 
}