phalcon/cphalcon · error · Phalcon\Filter\Validation\Exceptions\InvalidValidationData

Invalid data to validate

Error message

Invalid data to validate

What it means

When you pass $data to Validation::validate(), it must be an array or an object (entity-like source); null is treated as 'use previously bound data'. InvalidValidationData is thrown when a non-null scalar is passed — a string, int, float, or bool.

Source

Thrown at phalcon/Filter/Validation.zep:644

            combinedFieldsValidators = this->combinedFieldsValidators;

        if unlikely typeof validatorData != "array" {
            throw new NoValidators();
        }

        /**
         * Clear pre-calculated values
         */
        let this->values = [];

        /**
         * Implicitly creates a Phalcon\Messages\Messages object
         */
        let this->messages = new Messages();
        if (data !== null) {
            // if data is provided
            if unlikely typeof data != "array" && typeof data != "object" {
                throw new InvalidValidationData();
            }
            let this->data = data;
            let inputData = data;
        } elseif !empty this->data {
            // else, if data === null, but we have this->data from bind(), reuse this->data
            let inputData = this->data;
        }

        if entity !== null {
            // if user provided entity, bind and assign the data to the entity
            this->bind(entity, inputData, whitelist);
        }

        /**
         * Validation classes can implement the 'beforeValidation' callback
         */
        if method_exists(this, "beforeValidation") {
            let status = this->{"beforeValidation"}(inputData, this->entity, this->messages);

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Decode string payloads first: $validation->validate(json_decode($rawBody, true))
  2. Pass arrays ($_POST, model arrays, hydrator output) or an entity object
  3. For query strings parse them: parse_str($qs, $data)

Example fix

// before
$messages = $validation->validate($request->getRawBody()); // raw JSON string -> throws

// after
$data = json_decode($request->getRawBody(), true) ?: [];
$messages = $validation->validate($data);
Defensive patterns

Strategy: type-guard

Validate before calling

if ($data !== null && !is_array($data) && !is_object($data)) {
    throw new InvalidArgumentException('Validation data must be array|object|null, got ' . get_debug_type($data));
}
$messages = $validation->validate($data);

Type guard

function isPayloadCandidate($data): bool
{
    return $data === null || is_array($data) || is_object($data);
}

Try / catch

use Phalcon\Filter\Validation\Exceptions\InvalidValidationData;
try {
    $messages = $v->validate($payload);
} catch (InvalidValidationData $e) {
    $decoded = json_decode((string) $payload, true);
    $messages = is_array($decoded) ? $v->validate($decoded) : throw $e;
}

Prevention

When it happens

Trigger: $validation->validate($request->getRawBody()) passing the raw JSON string; validate('name=Tom&age=30') passing a query string; validate(0) or validate(true) from a mis-typed variable.

Common situations: Forgetting json_decode() on JSON request bodies in REST endpoints; passing a serialized string instead of the decoded array; template/request objects whose accessor returns a scalar being handed straight to validate().

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/2406d715ec618b9c. Report an issue: GitHub.