/**
  * @covers \airmoi\FileMaker\FileMakerValidationException::addError
  */
 public function testAddError()
 {
     $exception = new FileMakerValidationException($this->fm);
     $field = new Object\Field(new Object\Layout($this->fm));
     $field->name = "sample_field";
     $exception->addError($field, FileMaker::RULE_DATE_FIELD, "Incorrect date value");
     $this->assertEquals(1, $exception->numErrors());
     $field2 = new Object\Field(new Object\Layout($this->fm));
     $field2->name = "sample_field2";
     $exception->addError($field2, FileMaker::RULE_NOTEMPTY, null);
     $this->assertEquals(2, $exception->numErrors());
     return $exception;
 }
Exemple #2
0
 /**
  * Pre-validates either a single field or the entire command.
  *
  * This method uses the pre-validation rules that are enforceable by the
  * PHP engine -- for example, type rules, ranges, and four-digit dates.
  * Rules such as "unique" or "existing," or validation by calculation
  * field, cannot be pre-validated.
  *
  * If you pass the optional $fieldName argument, only that field is
  * pre-validated. Otherwise, the command is pre-validated as if execute()
  * were called with "Enable record data pre-validation" selected in
  * FileMaker Server Admin Console. If pre-validation passes, validate()
  * returns TRUE. If pre-validation fails, then validate() throws a
  * \airmoi\FileMaker\FileMakerValidationException object containing details about what failed
  * to pre-validate.
  *
  * @param string $fieldName Name of field to pre-validate. If empty,
  *                          pre-validates the entire command.
  *
  * @return bool TRUE, if pre-validation passes.
  * @throws FileMakerException
  * @throws FileMakerValidationException
  */
 public function validate($fieldName = null)
 {
     if (!is_a($this, Add::class) && !is_a($this, Edit::class)) {
         return true;
     }
     $layout = $this->fm->getLayout($this->_layout);
     $validationErrors = new FileMakerValidationException($this->fm);
     if ($fieldName === null) {
         foreach ($layout->getFields() as $fieldName => $field) {
             if (!isset($this->_fields[$fieldName]) || !count($this->_fields[$fieldName])) {
                 $values = array(0 => null);
             } else {
                 $values = $this->_fields[$fieldName];
             }
             foreach ($values as $value) {
                 try {
                     $field->validate($value);
                 } catch (FileMakerValidationException $e) {
                     foreach ($e->getErrors() as $error) {
                         $validationErrors->addError($error[0], $error[1], $error[2]);
                     }
                 }
             }
         }
     } else {
         $field = $layout->getField($fieldName);
         if (!isset($this->_fields[$fieldName]) || !count($this->_fields[$fieldName])) {
             $values = array(0 => null);
         } else {
             $values = $this->_fields[$fieldName];
         }
         foreach ($values as $value) {
             try {
                 $field->validate($value);
             } catch (FileMakerValidationException $e) {
                 foreach ($e->getErrors() as $error) {
                     $validationErrors->addError($error[0], $error[1], $error[2]);
                 }
             }
         }
     }
     if ($validationErrors->numErrors()) {
         if ($this->fm->getProperty('errorHandling') === 'default') {
             return $validationErrors;
         }
         throw $validationErrors;
     }
     return true;
 }
Exemple #3
0
 public function checkTimeValidity($value, $rule, FileMakerValidationException $validationError, $shortHoursFormat)
 {
     if ($shortHoursFormat) {
         $format = 12;
     } else {
         $format = 24;
     }
     preg_match('#([0-9]{1,2})[:]([0-9]{1,2})[:]?([0-9]{1,2})?#', $value, $matches);
     $hours = $matches[1];
     $minutes = $matches[2];
     if (count($matches) >= 4) {
         $seconds = $matches[3];
     }
     if ($hours < 0 || $hours > $format) {
         $validationError->addError($this, $rule, $value);
     } elseif ($minutes < 0 || $minutes > 59) {
         $validationError->addError($this, $rule, $value);
     } elseif (isset($seconds)) {
         if ($seconds < 0 || $seconds > 59) {
             $validationError->addError($this, $rule, $value);
         }
     }
 }