Kohana ORM Check if user exists and returning messages to View? -
Kohana ORM Check if user exists and returning messages to View? -
i'm using kohana 3.3 in project , i'm trying user registration , login working. using orm's auth , kostache managing layout/templates.
how i:
check if username exists? if homecoming error_msg.mustache message "user exists" check if username , email valid according model rules? if not homecoming error message error_msg.mustache indicating validation failedin controller have:
class controller_user extends controller { public function action_signup() { $renderer = kostache_layout::factory(); $this->response->body($renderer->render(new view_frontend_user, 'frontend/signup')); } public function action_createuser() { seek { $user = orm::factory('user'); $user->username = $this->request->post('username'); $user->password = $this->request->post('password'); $user->email = $this->request->post('email'); // how i: // check if username exists? if homecoming error_msg.mustache message "user exists" // check if email valid? if not homecoming error message error_msg.mustache indicating "email not valid" $user->save(); } grab (orm_validation_exception $e) { $errors = $e->errors(); } } }
in model:
<?php class model_user extends model_auth_user { public function rules() { homecoming array( 'username' => array( array('not_empty'), array('min_length', array(':value', 4)), array('max_length', array(':value', 32)), array('regex', array(':value', '/^[-\pl\pn_.]++$/ud')), ), 'email' => array( array('not_empty'), array('min_length', array(':value', 4)), array('max_length', array(':value', 127)), array('email'), ), ); } }
thanks lot in advance!
you can uniqueness check using validation , already-written callback. has advantages of keeping validation logic together, , of beingness concise:
public function rules() { homecoming array( 'username' => array( array(array($this, 'unique'), array(':field', ':value')), // ...
as simple that!
i answered question own solution, different pre-rolled version, know i'll utilize instead of this:
public function rules() { homecoming array( 'username' => array( // ... array('model_user::unique_field', array(':field', ':value', $this->pk())), ), // ... ); } public static function unique_field($field, $value, $user_id = null) { homecoming (orm::factory('user')->where($field, '=', $value)->find()->pk() === $user_id); }
kohana kohana-orm kostache
Comments
Post a Comment