Advertisement
Guest User

Untitled

a guest
Sep 12th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. /**
  2.  * Validator
  3.  */
  4. class DoForm_Validator extends RequiredFields {
  5.  
  6.     protected $id;
  7.     protected $customRequired = array('Number', 'Title', 'IssuingDate', 'Revision', 'RevisionDate');
  8.  
  9.     /**
  10.      * Constructor
  11.      * @param Int $id id of the DO we are validating, 0 for new ones
  12.      */
  13.     public function __construct($id = 0) {
  14.         Debug::log('ID2: ' . $this->id);
  15.         $this->id = $id;
  16.         parent::__construct($this->customRequired);
  17.     }
  18.  
  19.  
  20.     /**
  21.      * Verifica che il Number sia univoco
  22.      *
  23.      * @param array $data Submitted data
  24.      * @return bool Returns TRUE if the submitted data is valid, otherwise
  25.      *              FALSE.
  26.      */
  27.     public function php($data) {
  28.         $valid = parent::php($data);
  29.        
  30.         if ($valid) {
  31.             Debug::log(print_r($data, true));
  32.            
  33.             $check = DoForm::get('DoForm', "Number='{$data['Number']}'");
  34.            
  35.             Debug::log('this ID: ' . $this->id);
  36.             Debug::log('DB ID: ' . $check->First()->ID);
  37.             Debug::log('Check count: ' . $check->count());
  38.  
  39.             if ($this->id !== 0) {
  40.                 // Modifica di un record - l'unico DO presente deve avere lo stesso ID
  41.                 // di quello che sto modificando
  42.                 Debug::log('Modifica');
  43.                 if ($check->First()->ID !== $this->ID) {
  44.                     $this->validationError(
  45.                         'Number',
  46.                         "Esiste già il modulo numero {$data['Number']}",
  47.                         'validation'
  48.                     );
  49.                     $valid = false;
  50.                 }
  51.             } else {
  52.                 // Nuovo record - non dev'essere presente nessun DO con quel Number
  53.                 Debug::log('Inserimento');
  54.                 if ($check->count() > 0) {
  55.                     $this->validationError(
  56.                         'Number',
  57.                         "Esiste già il modulo numero {$data['Number']}",
  58.                         'validation'
  59.                     );
  60.                     $valid = false;
  61.                 }
  62.             }
  63.         }
  64.        
  65.         return $valid;
  66.     }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement