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

Entity must be an object

Error message

Entity must be an object

What it means

Validation::setEntity(entity) binds an object (typically a model) whose getters/setters supply field values during validation. ValidationEntityNotObject is thrown when anything other than an object is passed — null, array, string, or scalar.

Source

Thrown at phalcon/Filter/Validation.zep:548

    {
        var localMessages;

        let localMessages = self::defaultMessages;

        let self::defaultMessages = array_merge(localMessages, messages);

        return self::defaultMessages;
    }

    /**
     * Sets the bound entity
     *
     * @param object entity
     */
    public function setEntity(entity) -> void
    {
        if unlikely typeof entity != "object" {
            throw new ValidationEntityNotObject();
        }

        let this->entity = entity;
    }

    /**
     * Adds filters to the field
     *
     * @param string field
     * @param array|string filters
     */
    public function setFilters(var field, filters) -> <static>
    {
        var singleField;

        if typeof field == "array" {
            for singleField in field {
                let this->filters[singleField] = filters;

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Guard the null case before binding: if (!$user) { handle not-found; } else { $validation->setEntity($user); }
  2. For array data use bind(null, $arrayData) or validate($arrayData) instead of setEntity()
  3. Type-hint the caller: setEntity(object $entity) at your own wrapper

Example fix

// before
$user = User::findFirst($id); // null when missing
$validation->setEntity($user); // throws ValidationEntityNotObject

// after
$user = User::findFirst($id);
if ($user === null) {
    throw new NotFoundException("User {$id} not found");
}
$validation->setEntity($user);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is_object($entity)) {
    throw new InvalidArgumentException('Entity must be an object, got ' . get_debug_type($entity));
}
$validation->setEntity($entity);

Type guard

function isEntityCandidate($entity): bool
{
    return is_object($entity);
}

Try / catch

use Phalcon\Filter\Validation\Exceptions\ValidationEntityNotObject;
try {
    $v->setEntity($entity);
} catch (ValidationEntityNotObject $e) {
    // missing record or wrong payload shape: treat as not-found, not as 500
    throw new NotFoundException('Entity to validate does not exist', 0, $e);
}

Prevention

When it happens

Trigger: $validation->setEntity(User::findFirst($id)) where findFirst() returns null/false for a missing record; $validation->setEntity($request->getPost()) passing an array; setEntity($data['user'] ?? null) defaulting to null.

Common situations: Null-coalesced model fetches feeding setEntity; treating arrays as entities instead of using bind(); 'not found' branches that continue to the validation step.

Related errors


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