Advertisement
Guest User

Untitled

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