Advertisement
Guest User

Untitled

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