Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Users_Controller
- public function ajax_update()
- {
- if ($input = $this->input->post()) {
- $user = new User_Model(Session::get('user_id', FALSE));
- $profile = new Profile_Model(Session::get('user_id', FALSE));
- // I get an error because I try to pass $input to a second model (expecting an array) but it is now and object
- // would the best way to be use $input_user and $input_validate then merge error arrays?
- if ($user->validate($input, 'update') && $profile->validate($input)) {
- if ( ! empty($input['password']))
- $user->hashed_password = Auth::hash_password($input['password']);
- $user->save();
- $profile->save();
- $json = array('success' => TRUE);
- $this->template->body = json_encode($json);
- } else {
- foreach ($input->errors('form_errors') as $error) {
- $errors[] = $error;
- }
- $json = array('success' => FALSE, 'errors' => $errors);
- $this->template->body = json_encode($json);
- }
- }
- }
- ////////////////////////
- //////////////////////
- ///// User_Model extends ORM
- ///// has_one profile
- public function validate(array & $input, $action)
- {
- $input = new Validation($input);
- if ($action == 'update')
- $input->pre_filter('trim')
- ->add_rules('name', 'required')
- ->add_rules('email', 'required', 'valid::email')
- ->add_rules('email', array($this, '_unique_email'))
- ->add_rules('password', 'length[4,20]')
- ->add_rules('password', 'matches[confirm_password]');
- return parent::validate($input);
- }
- ////////////////////////
- //////////////////////
- /////
- class Profile_Model extends ORM {
- protected $belongs_to = array('user');
- protected $primary_key = 'user_id';
- public function validate(array & $validate)
- {
- $validate = new Validation($validate);
- $validate->pre_filter('trim')
- ->add_rules('phone_number', 'phone[10]')
- ->add_rules('major', 'standard_text')
- ->add_rules('website_url', 'url')
- ->add_rules('linkedin_url', 'url')
- ->add_rules('image_url', 'url')
- ->add_rules('bio', 'standard_text');
- return parent::validate($validate);
- }
- }
Add Comment
Please, Sign In to add comment