{"record":{"id":"db4d71636935cbf2","repo":"phalcon/cphalcon","slug":"field-must-be-passed-as-array-of-fields-or-string","errorCode":null,"errorMessage":"Field must be passed as array of fields or string","messagePattern":"Field must be passed as array of fields or string","errorType":"exception","errorClass":"Phalcon\\Filter\\Validation\\Exceptions\\InvalidFieldType","httpStatus":null,"severity":"error","filePath":"phalcon/Filter/Validation.zep","lineNumber":152,"sourceCode":"     * @return static\n     */\n    public function add(var field, <ValidatorInterface> validator) -> <static>\n    {\n        var singleField;\n\n        if typeof field === \"array\" {\n            // Uniqueness validator for combination of fields is handled differently\n            if validator instanceof AbstractCombinedFieldsValidator {\n                let this->combinedFieldsValidators[] = [field, validator];\n            } else {\n                for singleField in field {\n                    let this->validators[singleField][] = validator;\n                }\n            }\n        } elseif typeof field == \"string\" {\n            let this->validators[field][] = validator;\n        } else {\n            throw new InvalidFieldType();\n        }\n\n        return this;\n    }\n\n    /**\n     * Appends a message to the messages list\n     *\n     * @param MessageInterface $message\n     */\n    public function appendMessage(<MessageInterface> message) -> <static>\n    {\n        this->messages->appendMessage(message);\n\n        return this;\n    }\n\n    /**","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Filter/Validation.zep#L134-L170","documentation":"Validation::add(field, validator) accepts a single field name as a string, or a list of field names as an array (the array form with a combined-fields validator registers a scope). InvalidFieldType is thrown when field is any other type — int, float, bool, null, or object.","triggerScenarios":"$validation->add(0, new PresenceOf()) from looping over numeric-indexed input; $validation->add($config['field'], ...) where the config key is missing so null is passed; passing a field object instead of its name string.","commonSituations":"Field names taken from array_keys() of a numerically indexed payload; dynamic validator building from request data where the key does not exist; refactors that turn field constants into undefined values.","solutions":["Pass a string field name: $validation->add('email', new PresenceOf())","For several fields at once pass an array of strings: $validation->add(['email', 'name'], new PresenceOf())","When the name is dynamic, cast/verify it: is_string($field) || (is_array($field) && $field) before calling add()"],"exampleFix":"// before\nforeach ($row as $key => $value) {\n    $validation->add($key, new PresenceOf()); // $key = 0 throws InvalidFieldType\n}\n\n// after\nforeach ($row as $key => $value) {\n    if (is_string($key)) {\n        $validation->add($key, new PresenceOf());\n    }\n}","handlingStrategy":"type-guard","validationCode":"if (!is_string($field) && !is_array($field)) {\n    throw new InvalidArgumentException('Field must be string or array of strings, got ' . get_debug_type($field));\n}\n$validation->add($field, $validator);","typeGuard":"function isValidFieldName($field): bool\n{\n    if (is_string($field)) { return $field !== ''; }\n    return is_array($field) && $field !== [] && array_all($field, 'is_string');\n}","tryCatchPattern":"use Phalcon\\Filter\\Validation\\Exceptions\\InvalidFieldType;\ntry {\n    $validation->add($dynamicField, $validator);\n} catch (InvalidFieldType $e) {\n    throw new InvalidArgumentException('Rejected dynamic field name: ' . get_debug_type($dynamicField), 0, $e);\n}","preventionTips":["Never feed raw array_keys() of user data into add(); whitelist field names instead","Use string field constants shared between form definitions and validation setup"],"tags":["phalcon","validation","field-name","type-error"],"backgroundTag":"invalid-argument-type","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}