Ejemplo n.º 1
0
 public function _validate()
 {
     ValidationManager::validate(function () use($vm) {
         $vm->register('barcode_type', new FunctionValidator(function () {
             return in_array($this->type, self::$BARCODE_TYPES);
         }), 'Barcode type must be one of those: ', implode(', ', self::$BARCODE_TYPES));
         $vm->register('barcode', new FunctionValidator(function () {
             return empty(Barcode::findDuplicate($this->type, $this->value, $this->product));
         }), sprintf('There is a duplicated barcode of type %s with the value %s - Product: %s', $this->type, $this->value, $this->product->name));
         $required_length = 0;
         switch ($this->type) {
             case Barcode::ISBN_10:
                 $required_length = 10;
                 break;
             case Barcode::ISBN_13:
                 $required_length = 13;
                 break;
             case Barcode::ISSN:
                 $required_length = 8;
                 break;
         }
         $vm->register('barcode', new FunctionValidator(function () use($required_length) {
             return $required_length > 0 && strlen($this->value) == $required_length;
         }), 'ISSN must have length of 8');
     });
 }
Ejemplo n.º 2
0
 protected function _validate()
 {
     $vm = self::createValidationManager();
     $vm->register('name', new FunctionValidator(function ($name) {
         return !empty($name);
     }, array($this->name)));
     if ($this->getIsbn10()) {
         $vm->register('product[isbn_10]', new FunctionValidator(function () {
             return strlen($this->getIsbn10()->value) == 10;
         }), 'ISBN10 must have length of 10');
         $vm->register('product[isbn_10]', new FunctionValidator(function () {
             return empty(Barcode::findDuplicate(Barcode::ISBN_10, $this->getIsbn10()->value, $this));
         }), 'Duplicated ISBN10');
     }
     if ($this->getIsbn13()) {
         $vm->register('product[isbn_13]', new FunctionValidator(function () {
             return strlen($this->getIsbn13()->value) == 13;
         }), 'ISBN13 must have length of 13');
         $vm->register('product[isbn_13]', new FunctionValidator(function () {
             return empty(Barcode::findDuplicate(Barcode::ISBN_13, $this->getIsbn13()->value, $this));
         }), 'Duplicated ISBN13');
     }
     if ($this->getIssn()) {
         $vm->register('product[issn]', new FunctionValidator(function () {
             return strlen($this->getIssn()->value) == 8;
         }), 'ISSN must have length of 8');
         $vm->register('product[issn]', new FunctionValidator(function () {
             return empty(Barcode::findDuplicate(Barcode::ISSN, $this->getIssn()->value, $this));
         }), 'Duplicated ISSN');
     }
     $vm->register('product[price]', new Functionvalidator(function () {
         return $this->price > 0;
     }), 'Price must greater than 0');
     $vm->doValidate();
 }