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