Guest User

Untitled

a guest
Jun 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 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. // I get an error because I try to pass $input to a second model (expecting an array) but it is now and object
  11. // would the best way to be use $input_user and $input_validate then merge error arrays?
  12. if ($user->validate($input, 'update') && $profile->validate($input)) {
  13.  
  14. if ( ! empty($input['password']))
  15. $user->hashed_password = Auth::hash_password($input['password']);
  16.  
  17. $user->save();
  18. $profile->save();
  19.  
  20. $json = array('success' => TRUE);
  21. $this->template->body = json_encode($json);
  22.  
  23. } else {
  24.  
  25. foreach ($input->errors('form_errors') as $error) {
  26. $errors[] = $error;
  27. }
  28.  
  29. $json = array('success' => FALSE, 'errors' => $errors);
  30. $this->template->body = json_encode($json);
  31. }
  32. }
  33. }
  34.  
  35. ////////////////////////
  36. //////////////////////
  37. ///// User_Model extends ORM
  38. ///// has_one profile
  39.  
  40. public function validate(array & $input, $action)
  41. {
  42. $input = new Validation($input);
  43.  
  44. if ($action == 'update')
  45. $input->pre_filter('trim')
  46. ->add_rules('name', 'required')
  47. ->add_rules('email', 'required', 'valid::email')
  48. ->add_rules('email', array($this, '_unique_email'))
  49. ->add_rules('password', 'length[4,20]')
  50. ->add_rules('password', 'matches[confirm_password]');
  51.  
  52. return parent::validate($input);
  53. }
  54.  
  55. ////////////////////////
  56. //////////////////////
  57. /////
  58. class Profile_Model extends ORM {
  59. protected $belongs_to = array('user');
  60. protected $primary_key = 'user_id';
  61.  
  62. public function validate(array & $validate)
  63. {
  64. $validate = new Validation($validate);
  65.  
  66. $validate->pre_filter('trim')
  67. ->add_rules('phone_number', 'phone[10]')
  68. ->add_rules('major', 'standard_text')
  69. ->add_rules('website_url', 'url')
  70. ->add_rules('linkedin_url', 'url')
  71. ->add_rules('image_url', 'url')
  72. ->add_rules('bio', 'standard_text');
  73.  
  74. return parent::validate($validate);
  75. }
  76. }
Add Comment
Please, Sign In to add comment