phalcon/cphalcon · error · Phalcon\Tag\Exception
Only scalar values can be assigned to UI components
Error message
Only scalar values can be assigned to UI components
What it means
Phalcon\Tag::setDefault() seeds default values that tag helpers use to pre-fill form fields. Because every default is later echoed into HTML markup, only scalars are usable; an array or object value throws Phalcon\Tag\Exception ('Only scalar values can be assigned to UI components'). null is explicitly allowed and is the supported way to clear a default.
Source
Thrown at phalcon/Tag.zep:1122
return Select::selectField(parameters, data);
}
/**
* Set autoescape mode in generated HTML
*/
public static function setAutoescape(bool autoescape) -> void
{
let self::autoEscape = autoescape;
}
/**
* Assigns default values to generated tags by helpers
*/
public static function setDefault( string id, value) -> void
{
if value !== null {
if unlikely (typeof value == "array" || typeof value == "object") {
throw new Exception(
"Only scalar values can be assigned to UI components"
);
}
}
let self::displayValues[id] = value;
}
/**
* Assigns default values to generated tags by helpers
*
* @phpstan-param tag_display_values $values
*/
public static function setDefaults( array values, bool merge = false) -> void
{
if merge && typeof self::displayValues == "array" {
let self::displayValues = array_merge(self::displayValues, values);
} else {View on GitHub (pinned to b7419de9cd)
Solutions
- Assign the scalar field: Tag::setDefault('name', $user->name)
- For objects that must appear, format them to string first — cast or call a __toString()-bearing method
- If the value is a list, it belongs in the helper's options (e.g. select options), not in setDefault
Example fix
// before
\Phalcon\Tag::setDefault('category', $categories); // array -> throws
// after
\Phalcon\Tag::setDefault('category', (int) $request->get('category', 'int', 0)); Defensive patterns
Strategy: validation
Validate before calling
if (is_object($value) && method_exists($value, '__toString')) {
$value = (string) $value;
}
if (null === $value || is_scalar($value)) {
\Phalcon\Tag::setDefault('name', $value);
} Type guard
function isScalarTagDefault($value): bool
{
return null === $value || is_scalar($value);
} Prevention
- Store only string/int/float/bool defaults — typically filtered request input
- Assign model fields ($user->name), never model objects, as defaults
- Clear defaults deliberately with null (supported), never by assigning arrays or objects
When it happens
Trigger: Tag::setDefault('category', ['fruits', 'vegetables']); Tag::setDefault('user', $user) — assigning an object/Resultset instead of a field; arrays leaking in from request->getPost() when a field was submitted multiple times.
Common situations: Pre-filling a select's default with its options array instead of the selected key; assigning model objects rather than model properties; defaults sourced from decoded JSON whose values are nested arrays.
Related errors
- The 'using' parameter is required
- Form '{name}' is not registered in the FormsLocator
- Unknown form element type "{type}"
- Form schema definition at index {index} must be an array
- Form schema definition at index {index} is missing required
AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21).
Data as JSON: /api/errors/c7fb27c659789ead.
Report an issue: GitHub.