/** * Specify data which should be serialized to JSON. * * @link http://php.net/manual/en/jsonserializable.jsonserialize.php * @return mixed data which can be serialized by <b>json_encode</b>, * which is a value of any type other than a resource. */ public function jsonSerialize() { $errors = $this->validation->getErrorsFlat(); if (count($errors) === 1) { $message = Validation::errorMessage($errors[0]); } else { $message = t('Validation failed.'); } $result = ['message' => $message, 'status' => $this->getCode(), 'errors' => $errors]; return $result; }
/** * Validate a required field. * * @param mixed &$value The field value. * @param array $field The field definition. * @param Validation $validation A {@link Validation} object to collect errors. * @return bool|null Returns one of the following: * - null: The field is not required. * - true: The field is required and {@link $value} is not empty. * - false: The field is required and {@link $value} is empty. */ protected function validateRequired(&$value, array $field, Validation $validation) { $required = val('required', $field, false); $type = $field['type']; if ($value === '' || $value === null) { if (!$required) { $value = null; return true; } switch ($type) { case 'boolean': $value = false; return true; case 'string': if (val('minLength', $field, 1) == 0) { $value = ''; return true; } } $validation->addError('missing_field', $field); return false; } return null; }