{"record":{"id":"57d35c3d62052347","repo":"twigphp/Twig","slug":"the-s-attribute-value-should-be-a-scalar-an-iterable-or-an","errorCode":null,"errorMessage":"The \"%s\" attribute value should be a scalar, an iterable, or an object implementing \"%s\", got \"%s\".","messagePattern":"The \"(.+?)\" attribute value should be a scalar, an iterable, or an object implementing \"(.+?)\", got \"(.+?)\"\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"extra/html-extra/HtmlExtension.php","lineNumber":283,"sourceCode":"            $value = $value->getValue();\n        }\n\n        // In general, ...\n        if (true === $value) {\n            // ... use attribute=\"\" for boolean true,\n            // which is XHTML compliant and indicates the \"empty value default\", see\n            // https://html.spec.whatwg.org/multipage/syntax.html#attributes-2 and\n            // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes\n            $value = '';\n        }\n\n        if (null === $value || false === $value) {\n            // omit null-valued and false attributes completely (note aria-* has been processed before)\n            return null;\n        }\n\n        if (\\is_object($value) && !$value instanceof \\Stringable) {\n            throw new RuntimeError(\\sprintf('The \"%s\" attribute value should be a scalar, an iterable, or an object implementing \"%s\", got \"%s\".', $name, \\Stringable::class, get_debug_type($value)));\n        }\n\n        return (string) $value;\n    }\n}\n","sourceCodeStart":265,"sourceCodeEnd":289,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/extra/html-extra/HtmlExtension.php#L265-L289","documentation":"When rendering an attribute, htmlAttrValue() omits null/false values, stringifies scalars/iterables/Stringable objects, but refuses objects that are neither Stringable nor iterable-friendly — there is no safe string representation, so it throws with the attribute name, the expected interface and the actual debug type.","triggerScenarios":"Passing an object without __toString() (e.g. a plain stdClass, DateTime, or entity) as an attribute value to html_attr().","commonSituations":"Passing model entities or DateTime directly into attributes; refactors replacing scalar values with objects; forgetting to cast dates with ->format().","solutions":["Implement Stringable (__toString) on the value class.","Convert explicitly: $date->format(DATE_ATOM), (string) $id, etc.","Pass null or false instead of an empty object if the attribute should be omitted.","Wrap the object in an array only if a JSON list representation is desired."],"exampleFix":"// before\nhtml_attr(['data-date' => $createdAt])\n// after\nhtml_attr(['data-date' => $createdAt->format(DATE_ATOM)])","handlingStrategy":"type-guard","validationCode":"foreach ($attrs as $name => $v) { if (is_object($v) && !$v instanceof \\Stringable && !is_iterable($v)) throw new \\TypeError(\"$name must be scalar/iterable/Stringable\"); }","typeGuard":"function isRenderableAttrValue(mixed $v): bool { return null === $v || false === $v || is_scalar($v) || is_iterable($v) || $v instanceof \\Stringable; }","tryCatchPattern":"try { $html = html_attr($attrs); } catch (Twig\\Error\\RuntimeError $e) { $attrs = array_map(fn($v) => is_object($v) && !$v instanceof \\Stringable ? (method_exists($v,'__toString') ? (string)$v : null) : $v, $attrs); $html = html_attr($attrs); }","preventionTips":["Implement __toString on value objects used in attributes","Format dates explicitly with ->format()","Omit attributes with null/false instead of empty objects"],"tags":["twig","type-error","html-attr"],"backgroundTag":"invalid-argument-value","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}