Fuel\Validation\Validator::validateField PHP Method

validateField() protected method

Validates a single field
Since: 2.0
protected validateField ( string $field, mixed[] $data, Fuel\Validation\ResultInterface $result ) : boolean
$field string
$data mixed[]
$result Fuel\Validation\ResultInterface
return boolean
    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;
    }