Ejemplo n.º 1
0
 /**
  * Validate a field.
  *
  * @param mixed &$value The value to validate.
  * @param array $field Parameters on the field.
  * @param Validation $validation A validation object to add errors to.
  * @throws \InvalidArgumentException Throws an exception when there is something wrong in the {@link $params}.
  * @internal param string $fieldname The name of the field to validate.
  * @return bool Returns true if the field is valid, false otherwise.
  */
 protected function validateField(&$value, array $field, Validation $validation)
 {
     $path = self::arraySelect(['path', 'name'], $field);
     $type = val('type', $field, '');
     $valid = true;
     // Check required first.
     // A value that isn't passed should fail the required test, but short circuit the other ones.
     $validRequired = $this->validateRequired($value, $field, $validation);
     if ($validRequired !== null) {
         return $validRequired;
     }
     // Validate the field's type.
     $validType = true;
     switch ($type) {
         case 'boolean':
             $validType &= $this->validateBoolean($value, $field, $validation);
             break;
         case 'integer':
             $validType &= $this->validateInteger($value, $field, $validation);
             break;
         case 'float':
             $validType &= $this->validateFloat($value, $field, $validation);
             break;
         case 'string':
             $validType &= $this->validateString($value, $field, $validation);
             break;
         case 'timestamp':
             $validType &= $this->validateTimestamp($value, $field, $validation);
             break;
         case 'datetime':
             $validType &= $this->validateDatetime($value, $field, $validation);
             break;
         case 'base64':
             $validType &= $this->validateBase64($value, $field, $validation);
             break;
         case 'array':
             $validType &= $this->validateArray($value, $field, $validation);
             break;
         case 'object':
             $validType &= $this->validateObject($value, $field, $validation);
             break;
         case '':
             // No type was specified so we are valid.
             $validType = true;
             break;
         default:
             throw new \InvalidArgumentException("Unrecognized type {$type}.", 500);
     }
     if (!$validType) {
         $valid = false;
         $validation->addError('invalid_type', $path, ['type' => $type, 'message' => sprintft('%1$s is not a valid %2$s.', $path, $type), 'status' => 422]);
     }
     // Validate a custom field validator.
     $validatorName = val('validatorName', $field, $path);
     if (isset($this->validators[$validatorName])) {
         foreach ($this->validators[$validatorName] as $callback) {
             call_user_func_array($callback, [&$value, $field, $validation]);
         }
     }
     return $valid;
 }
Ejemplo n.º 2
0
 /**
  * Get the message for this exception.
  *
  * @return string Returns the exception message.
  */
 public function getMessage()
 {
     if ($this->mainMessage) {
         return $this->mainMessage;
     }
     // Generate the message by concatenating all of the errors together.
     $messages = [];
     foreach ($this->errors as $errors) {
         foreach ($errors as $error) {
             $field = val('field', $error, '*');
             if (is_array($field)) {
                 $field = implode(', ', $field);
             }
             if (isset($error['message'])) {
                 $message = $error['message'];
             } elseif (strpos($error['code'], '%s') === false) {
                 $message = sprintft($error['code'] . ': %s.', $field);
             } else {
                 $message = sprintft($error['code'], $field);
             }
             $messages[] = $message;
         }
     }
     return implode(' ', $messages);
 }