dotnet/aspnetcore · error · Error

Invalid boolean value '${attributeValue}' for parameter '${p

Error message

Invalid boolean value '${attributeValue}' for parameter '${parameterName}'

What it means

Thrown by BlazorCustomElement.parseAttributeValue when a custom element attribute is declared as a boolean parameter but the attribute string is not one of the accepted literals ('true','True','false','False'). Blazor custom elements coerce HTML attribute strings (which are always strings) into the .NET parameter type, and a boolean parameter only accepts those four spellings.

Source

Thrown at src/Components/CustomElements/src/js/BlazorCustomElements.ts:120

        await setParametersPromise;
      }
    }
  }

  static parseAttributeValue(attributeValue: string, type: JSComponentParameterType, parameterName: string): any {
    switch (type) {
      case 'string':
        return attributeValue;
      case 'boolean':
        switch (attributeValue) {
          case 'true':
          case 'True':
            return true;
          case 'false':
          case 'False':
            return false;
          default:
            throw new Error(`Invalid boolean value '${attributeValue}' for parameter '${parameterName}'`);
        }
      case 'number':
        const number = Number(attributeValue);
        if (Number.isNaN(number)) {
          throw new Error(`Invalid number value '${attributeValue}' for parameter '${parameterName}'`);
        } else {
          return number;
        }
      case 'boolean?':
        return attributeValue ? BlazorCustomElement.parseAttributeValue(attributeValue, 'boolean', parameterName) : null;
      case 'number?':
        return attributeValue ? BlazorCustomElement.parseAttributeValue(attributeValue, 'number', parameterName) : null;
      case 'object':
        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.`);
      default:
        throw new Error(`Unknown type '${type}' for parameter '${parameterName}'`);
    }
  }

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Use one of the accepted spellings: 'true'/'True'/'false'/'False' (see BlazorCustomElements.ts:113-118).
  2. If you need a nullable boolean, declare the parameter as 'boolean?' so an empty string is treated as null instead of throwing.
  3. Set the value as a JS property instead of an attribute: element.enabled = true; (this bypasses string parsing).
  4. Coerce in your templating layer before emitting the attribute, emitting 'true'/'false' literally.

Example fix

// before
<my-counter enabled="1"></my-counter>

// after
<my-counter enabled="true"></my-counter>
Defensive patterns

Strategy: validation

Validate before calling

const BOOL_LITERALS = new Set(['true', 'True', 'false', 'false'] as const);
const BOOL_OK = new Set(['true', 'True', 'false', 'False']);
function isValidBoolAttr(v: string): boolean {
  return BOOL_OK.has(v);
}

// before setting:
if (!isValidBoolAttr(value)) {
  throw new Error(`Refusing to set non-boolean attribute: ${value}`);
}
el.setAttribute('enabled', value);

Type guard

function asBoolAttr(v: unknown): 'true' | 'True' | 'false' | 'False' | null {
  return v === 'true' || v === 'True' || v === 'false' || v === 'False' ? (v as any) : null;
}

Try / catch

try {
  el.setAttribute('enabled', raw);
} catch (e) {
  if (/Invalid boolean value/.test((e as Error).message)) {
    console.warn('Skipping invalid boolean attribute', raw);
  } else throw e;
}

Prevention

When it happens

Trigger: Setting a boolean [Parameter] via an HTML attribute with an unsupported value: <my-component enabled="1"/>, enabled="yes", enabled="", or enabled="on". The attributeChangedCallback routes the value to parseAttributeValue which hits the 'boolean' case default branch.

Common situations: Writing attributes by hand with truthy shorthand ('1'/'yes'), passing JSON-style values, copy-pasting from a different component that accepted numbers, or templating engines that emit presence-only attributes as empty strings.

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/f37f825ee244825b. Report an issue: GitHub.