Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.13 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. }
  13.  
  14. $user = ORM::factory('user');
  15.  
  16. // this is the array of values that will be set
  17. $expected = array(
  18.     'username',
  19.     'password',
  20.     // ORM will automatically look for related models
  21.     'profile' => array(
  22.         // Notice that the address will not be saved
  23.         'name',
  24.         'aim',
  25.     ),
  26. );
  27.  
  28. $user->values($_POST['user'], $expected);
  29. try{
  30.     $user->save(); 
  31. }catch (ORM_Validation_Exception $e){
  32.     $errors = $e->errors();
  33. }
  34.  
  35. // the errors array will look something like this (including an example with many to many errors)
  36. $errors = {
  37.     user: {
  38.         username: 'This field is required.'
  39.         password: 'This field must be at least 6 characters long.',
  40.         profile: {
  41.             aim: 'This is not a valid AOL AIM screen name.'
  42.         }
  43.         occupations: [
  44.             {
  45.                 'job_title': 'This is not a valid job title',
  46.                 'description': 'This field is required'
  47.             },
  48.             {
  49.                 'job_title': 'This field is required'
  50.             },
  51.         ]
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement