Cfargument: required and default attributes

Written on 11 January 2012, 03:30pm

Tagged with:

Just noting an interesting observation about the attributes of the cfargument tag.
What happens if you set the required="true" attribute, but also the default attribute?
The answer is in the documentation: if passed, the ‘default‘ attribute is used, and the ‘required‘ attribute is ignored.

required (optional, default=’no’):
Specifies whether the parameter is required to execute the component method. The parameter is not required if you specify a default attribute.

An example below:


	<cffunction name="add">
		<cfargument name="a" required="true" type="string" default="1" />
		<cfargument name="b" required="true" type="string" default="1" />
		<cfreturn a+b>


<cfinvoke component="dummy"
	returnVariable="sum" />

The example below will still output 2, even if we call the ‘add’ method without any argument. The default values are used.

Leave a response