Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. $_POST = {
  2.     user = {
  3.         username: zeelot3k
  4.         password: awesomesauce
  5.         // Alias that is in the user model that points to user_profile model
  6.         profile = {
  7.             name: Lorenzo Pisani
  8.             address: 550 awesome street
  9.             aim: zeelot3k
  10.         }
  11.     }
  12.     _external = {
  13.         confirm-password: notawesome
  14.     }
  15. }
  16.  
  17. $user = ORM::factory('user');
  18.  
  19. // confirm-password validation belongs in the controller (it is not business logic, so we need to copy the password value to this _external array)
  20. $external_validation = Validate::factory($_POST['_external'] + array('password' => $_POST['user']['password']))
  21.     // This can only be done in the controller because the password in the model is ALWAYS hashed with __set filters
  22.     ->rule('confirm-password', 'matches', array('password'));
  23.  
  24. // this is the array of values that will be set
  25. $expected = array(
  26.     'username',
  27.     'password',
  28.     // ORM will automatically look for related models
  29.     'profile' => array(
  30.         // Notice that the address will not be saved
  31.         'name',
  32.         'aim',
  33.     ),
  34. );
  35.  
  36. $user->values($_POST['user'], $expected);
  37. try{
  38.     $user->save(); 
  39. }catch (ORM_Validation_Exception $e){
  40.     $errors = $e->errors();
  41. }
  42.  
  43. // the errors array will look something like this (including an example with many to many errors)
  44. $errors = {
  45.     user: {
  46.         username: 'This field is required.'
  47.         password: 'This field must be at least 6 characters long.',
  48.         profile: {
  49.             aim: 'This is not a valid AOL AIM screen name.'
  50.         }
  51.         occupations: [
  52.             {
  53.                 'job_title': 'This is not a valid job title',
  54.                 'description': 'This field is required'
  55.             },
  56.             {
  57.                 'job_title': 'This field is required'
  58.             },
  59.         ]
  60.     }
  61.     _external = {
  62.         'confirm-password': 'This field must match the password field.'
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement