{"record":{"id":"f37f825ee244825b","repo":"dotnet/aspnetcore","slug":"invalid-boolean-value-attributevalue-for-para","errorCode":null,"errorMessage":"Invalid boolean value '${attributeValue}' for parameter '${parameterName}'","messagePattern":"Invalid boolean value '(.+?)' for parameter '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/Components/CustomElements/src/js/BlazorCustomElements.ts","lineNumber":120,"sourceCode":"        await setParametersPromise;\n      }\n    }\n  }\n\n  static parseAttributeValue(attributeValue: string, type: JSComponentParameterType, parameterName: string): any {\n    switch (type) {\n      case 'string':\n        return attributeValue;\n      case 'boolean':\n        switch (attributeValue) {\n          case 'true':\n          case 'True':\n            return true;\n          case 'false':\n          case 'False':\n            return false;\n          default:\n            throw new Error(`Invalid boolean value '${attributeValue}' for parameter '${parameterName}'`);\n        }\n      case 'number':\n        const number = Number(attributeValue);\n        if (Number.isNaN(number)) {\n          throw new Error(`Invalid number value '${attributeValue}' for parameter '${parameterName}'`);\n        } else {\n          return number;\n        }\n      case 'boolean?':\n        return attributeValue ? BlazorCustomElement.parseAttributeValue(attributeValue, 'boolean', parameterName) : null;\n      case 'number?':\n        return attributeValue ? BlazorCustomElement.parseAttributeValue(attributeValue, 'number', parameterName) : null;\n      case 'object':\n        throw new Error(`The parameter '${parameterName}' accepts a complex-typed object so it cannot be set using an attribute. Try setting it as a element property instead.`);\n      default:\n        throw new Error(`Unknown type '${type}' for parameter '${parameterName}'`);\n    }\n  }","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/CustomElements/src/js/BlazorCustomElements.ts#L102-L138","documentation":"Thrown by BlazorCustomElement.parseAttributeValue when a custom-element attribute declared as type 'boolean' receives a value that is not exactly 'true', 'True', 'false', or 'False'. The parser is case-specific (only the leading-T variants are accepted) and rejects empty strings, '1'/'0', and any other truthy/falsy spelling.","triggerScenarios":"Setting an HTML attribute on a Blazor custom element whose [Parameter] is bool, with a value like \"\", \"1\", \"0\", \"yes\", \"TRUE\", or \"Disabled\". Triggered both by static markup and by attributeChangedCallback after setAttribute at runtime.","commonSituations":"HTML boolean attributes written minimally (e.g. <my-comp disabled>) yield attribute value \"\"; passing \"1\"/\"0\" from data-binding code; case differences like \"TRUE\"; binding a non-bool model field to a bool parameter.","solutions":["Use one of the four accepted spellings: 'true', 'True', 'false', 'False' as the attribute string.","For presence-style boolean attributes, set the value to 'true'/'false' explicitly rather than relying on the empty-string shorthand.","If the value comes from data binding, coerce it before assignment: value ? 'true' : 'false'.","If you need to pass a nullable or tri-state value, declare the parameter as 'boolean?' on the JS side so empty values map to null."],"exampleFix":"<!-- before -->\n<my-counter disabled></my-counter>\n<my-counter enabled=\"1\"></my-counter>\n\n<!-- after -->\n<my-counter disabled=\"true\"></my-counter>\n<my-counter enabled=\"true\"></my-counter>","handlingStrategy":"validation","validationCode":"// Coerce before setting an attribute that maps to a boolean parameter.\nfunction toBooleanAttr(value) {\n  const s = String(value).trim();\n  if (s === 'true' || s === 'True' || s === 'false' || s === 'False') return s;\n  // fall back to canonical spelling\n  return value ? 'true' : 'false';\n}\nel.setAttribute('enabled', toBooleanAttr(rawValue));","typeGuard":"function isValidBooleanAttr(v) {\n  return v === 'true' || v === 'True' || v === 'false' || v === 'False';\n}","tryCatchPattern":"try {\n  el.setAttribute('enabled', raw);\n} catch (e) {\n  if (/Invalid boolean value/.test(e.message)) {\n    el.setAttribute('enabled', raw ? 'true' : 'false');\n  } else throw e;\n}","preventionTips":["Always write explicit 'true'/'false' rather than empty-string boolean attributes.","Centralize boolean-to-attribute coercion in a helper.","Use 'boolean?' parameters when empty/unset is meaningful.","Add unit tests asserting accepted spellings for boolean attributes."],"tags":["blazor-custom-elements","attribute-parsing","validation","boolean"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}