Dec 30, 2011

Can't assign a default value to apex:attribute of type boolean

A couple of weeks ago I wrote a component that included several apex attribute tags. One of them was a flag that tells the control if it should be shown in a "disabled" mode. The normal mode for the control is enabled so I set the Type attribute to "boolean" and the Default attribute to "false". When I saved the control, I got the error "Literal value is required for attribute". At first, I didn't know what it was talking about, but after some experimentation, I realized that the Default attribute was causing the problem. I tried 0/1 and several true/false capitalizations, but nothing worked. That led me to the conclusion that in the case of boolean apex:attribute tags, SF does not cast the defaule value it gets into a boolean correctly (it expects a boolean value but tries to put a literal in it).

I still needed that to work, so instead of using the Default attribute, I set up a property in the class to handle the default value, and it worked like a charm...
Visualforce page:

<apex:attribute name="enabled" type="boolean" required="false" assignTo="{!controlEnabled}" />


Class:

public boolean controlEnabled { get { if(controlEnabled == null) controlEnabled = true; return controlEnabled; } set; }