{"record":{"id":"c98e389d2916914b","repo":"karatelabs/karate","slug":"ka-dispatch-requires-an-event-name-got-attributevalue","errorCode":null,"errorMessage":"ka:dispatch requires an event name; got `${attributeValue}`","messagePattern":"ka:dispatch requires an event name; got `(.+?)`","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/markup/KaDispatchProcessor.java","lineNumber":103,"sourceCode":"        String eventName = av;\n        String triggerOn = null;\n        int at = av.indexOf('@');\n        if (at >= 0) {\n            if (av.lastIndexOf('@') != at) {\n                throw new RuntimeException(\n                        \"ka:dispatch supports a single `event @ trigger` delimiter; got `\"\n                                + attributeValue + \"`\");\n            }\n            eventName = av.substring(0, at).trim();\n            triggerOn = av.substring(at + 1).trim();\n            if (triggerOn.isEmpty()) {\n                throw new RuntimeException(\n                        \"ka:dispatch trigger must be non-empty after `@`; got `\"\n                                + attributeValue + \"`\");\n            }\n        }\n        if (eventName.isEmpty()) {\n            throw new RuntimeException(\n                    \"ka:dispatch requires an event name; got `\"\n                            + attributeValue + \"`\");\n        }\n        MarkupTemplateContext kec = (MarkupTemplateContext) ctx;\n        String detailJson = \"{}\";\n        String vals = tag.getAttributeValue(getDialectPrefix(), VALS);\n        if (vals != null && !vals.isEmpty()) {\n            Object result = kec.evalLocalAsObject(vals);\n            if (result instanceof Map) {\n                detailJson = Json.of(result).toString();\n            } else if (result != null) {\n                logger.warn(\"ka:dispatch ignored ka:vals — did not evaluate to an object: {}\", vals);\n            }\n        }\n        String js = \"window.dispatchEvent(new CustomEvent(\\\"\"\n                + escapeJsString(eventName)\n                + \"\\\", {detail: \" + detailJson + \", bubbles: true, composed: true}))\";\n        if (triggerOn != null) {","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/markup/KaDispatchProcessor.java#L85-L121","documentation":"The ka:dispatch custom element forwards a DOM/browser event with a name given after the `@` in its attribute value. Karate's markup engine throws this RuntimeException when the element has a `ka:dispatch` attribute whose value has no event name part (empty string, or the part before/after `@` is blank), so the processor cannot determine which event to dispatch.","triggerScenarios":"Rendering a template containing `<div ka:dispatch=\"\">` or a value like `ka:dispatch=\"click@\"` / `ka:dispatch=\"@\"` where the event-name segment (after `@`) is empty after trimming.","commonSituations":"Typos or dynamically built attribute values whose variable is empty at render time (e.g. `th:attr=\"ka:dispatch=${eventName}\"` with an unset/empty model variable); copy-pasting examples and deleting the event name; refactoring templates so the event constant is lost.","solutions":["Set a non-empty event name in the attribute value: `ka:dispatch=\"submit@form-id\"` or the dialect-specific form used by your templates.","If the value comes from a model variable, ensure it is populated before render or provide a default: `th:attr=\"ka:dispatch=${eventName ?: 'click'}\"`.","Inspect the rendered template source at the failing line to confirm which element carries the empty ka:dispatch attribute.","If the element should not dispatch at all, remove the ka:dispatch attribute entirely rather than leaving it empty."],"exampleFix":"// before\n<div ka:dispatch=\"@send-btn\"></div>\n\n// after\n<div ka:dispatch=\"click@send-btn\"></div>","handlingStrategy":"validation","validationCode":"String v = tag.getAttributeValue(\"ka:dispatch\");\nif (v == null || v.trim().isEmpty() || v.trim().split(\"@\")[0].trim().isEmpty()) {\n    throw new IllegalArgumentException(\"ka:dispatch needs an event name, got: \" + v);\n}","typeGuard":null,"tryCatchPattern":"try {\n    render(template);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"ka:dispatch\")) {\n        log.error(\"Bad ka:dispatch attribute: {}\", e.getMessage());\n    }\n    throw e;\n}","preventionTips":["Always include a concrete event name in ka:dispatch values","Default dynamic values with `${eventName ?: 'click'}`","Add a render-every-template unit test to catch empty attributes early","Lint templates for ka:dispatch attributes with empty or missing event names"],"tags":["markup","thymeleaf","template","validation"],"backgroundTag":"empty-required-field","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}