Flex: using logical conjunction in an MXML attribute

Posted by admin at 12 February 2009

Category: programming

Tags: , , ,

If you try to use the logical AND operator inside of an MXML attribute to determine its value, the compiler will throw this error:

The entity name must immediately follow the ‘&’ in the entity reference

The error message is sketchy, especially given the fact that the logical OR operator is allowed.
The ampersand needs to be html-encoded (after all, MXML is an XML-based markup language!)

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
		[Bindable] private var arg1:Boolean;
		[Bindable] private var arg2:Boolean;

		private function validate():Boolean {
			return (this.arg1 && this.arg2);
		}
		]]>
	</mx:Script>

	<!-- bad -->
	<mx:Button enabled="{(this.arg1) && (this.arg2)}" />

	<!-- good -->
	<mx:Button enabled="{(this.arg1) &amp;&amp; (this.arg2)}" />

	<!-- also good, but more code -->
	<mx:Button enabled="{this.validate()}" />
</mx:Application>

Leave a Reply

Leave a Reply
  • (required)
  • (required) (will not be published)