Guest User

Untitled

a guest
Jun 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. // Users_Controller
  2.  
  3. public function ajax_update()
  4. {
  5. if ($input = $this->input->post()) {
  6.  
  7. $user = new User_Model(Session::get('user_id', FALSE));
  8. $profile = new Profile_Model(Session::get('user_id', FALSE));
  9.  
  10. // would the best way to be use $input_user and $input_validate then merge error arrays?
  11. if ($user->validate($input, 'update') && $profile->validate($input)) {
  12.  
  13. if ( ! empty($input['password']))
  14. $user->hashed_password = Auth::hash_password($input['password']);
  15.  
  16. $user->save();
  17. $profile->save();
  18.  
  19. $json = array('success' => TRUE);
  20. $this->template->body = json_encode($json);
  21.  
  22. } else {
  23.  
  24. foreach ($input->errors('form_errors') as $error) {
  25. $errors[] = $error;
  26. }
  27.  
  28. $json = array('success' => FALSE, 'errors' => $errors);
  29. $this->template->body = json_encode($json);
  30. }
  31. }
  32. }
  33.  
  34. ////////////////////////
  35. //////////////////////
  36. ///// User_Model extends ORM
  37.  
  38. public function validate(array & $input, $action)
  39. {
  40. $input = new Validation($input);
  41.  
  42. if ($action == 'update')
  43. $input->pre_filter('trim')
  44. ->add_rules('name', 'required')
  45. ->add_rules('email', 'required', 'valid::email')
  46. ->add_rules('email', array($this, '_unique_email'))
  47. ->add_rules('password', 'length[4,20]')
  48. ->add_rules('password', 'matches[confirm_password]');
  49.  
  50. return parent::validate($input);
  51. }
Add Comment
Please, Sign In to add comment