/** * Validates a single field * * @param string $field * @param mixed[] $data * @param ResultInterface $result * * @return bool * * @since 2.0 */ protected function validateField($field, $data, ResultInterface $result) { $value = null; // If there is data, and the data is not empty and not numeric. This allows for strings such as '0' to be passed // as valid values. $dataPresent = isset($data[$field]) && !(empty($data[$field]) && !is_numeric($data[$field])); if ($dataPresent) { $value = $data[$field]; } $rules = $this->getFieldRules($field); foreach ($rules as $rule) { if (!$dataPresent && !$rule->canAlwaysRun()) { continue; } $validateResult = $rule->validate($value, $field, $data); if ($validateResult instanceof ResultInterface) { $result->merge($validateResult, $field . '.'); return $validateResult->isValid(); } if (!$validateResult) { // Don't allow any others to run if this one failed $result->setError($field, $this->buildMessage($this->getField($field), $rule, $value), $rule); return false; } } // All is good so make sure the field gets added as one of the validated fields $result->setValidated($field); return true; }