Advertisement
d4rky

Model_Box

Dec 7th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.39 KB | None | 0 0
  1. <?php
  2. class Model_Box extends ORM
  3. {
  4.     public $_belongs_to = array('product' => array());
  5.  
  6.     public function formo()
  7.     {
  8.         return array
  9.         (
  10.             'id' => array(
  11.                 'editable' => FALSE,
  12.                 'render' => FALSE,
  13.             ),
  14.             'product' => array(
  15.                 'orm_primary_val' => 'name',
  16.                 'editable' => FALSE
  17.             ),
  18.             'filename' => array(
  19.                 'driver' => 'file'
  20.             )
  21.         );
  22.     }
  23.  
  24.     public function rules()
  25.     {
  26.         return array
  27.         (
  28.             'name' => array
  29.             (
  30.                 array('not_empty')
  31.             ),
  32.             'product_id' => array
  33.             (
  34.                 array('not_empty')
  35.             )
  36.         );
  37.     }
  38.  
  39.     // couldn't accomplish that otherwise, don't remember why though
  40.     // something about validation fscking up everywhere like crazy
  41.     // (could be my mistake though)
  42.     // this method is run in _create_passed() callback in my kohana3-crud (I already had the code up and running when you coded callbacks into formo)
  43.     public function validate_files($form)
  44.     {
  45.         $valid = new Validation($_FILES);
  46.         $valid->rules('filename', array(
  47.             array('Upload::not_empty', NULL),
  48.             array('Upload::type', array(':value', array('jpg','png','gif'))),
  49.             array('Upload::size', array(':value', '4M'))
  50.         ));
  51.  
  52.         try
  53.         {
  54.             $this->check($valid);
  55.         }
  56.         catch(ORM_Validation_Exception $e)
  57.         {
  58.             $errors = $e->errors();
  59.             foreach($errors['_external'] as $field => $error)
  60.             {
  61.                 $form->$field->error($error[0]);
  62.             }
  63.             return false;
  64.         }
  65.         return true;
  66.     }
  67.  
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement