Skip to main content
Inspiring
December 4, 2008
Question

Creating form element from within a component

  • December 4, 2008
  • 2 replies
  • 638 views
Hi all,

I am trying to create a form element within a component function which is then called from a .cfm page which has the cfform tags. I keep getting a context validation error for the form element tag. Here is some sample code:

<cfcomponent name="yourComponent">
<cffunction name="createTag" returntype="string" access="public">

<cfsavecontent variable="returnContent">
<cfselect name="thisSelect">
<option value="0">Value 1</option>
</cfselect>
</cfsaveconten>

<cfreturn returnContent>
</cffunction>
</cfcomponent>


And in the .cfm page:

<cfform>
<cfinvoke component="yourComponent" method="createTag" returnVariable="returnContent"/>
<cfoutput>#returnContent#</cfoutput>
</cfform>

The error occurs in the function and it says the cfselect must be within a cfform. I tried the function without the save content and just set the output="yes" but the same error occurs.

Any help appreciated in how to get around this. Just putting the code in the .cfm is not an option for this.

Any help appreciated.
Jim
This topic has been closed for replies.

2 replies

Inspiring
December 5, 2008
i see the same behaviour.
even with <cfsavecontent><cfselect ...>...</cfselect></cfsavecontent> in
the same .cfm page as the form (but outside the <cfform> tag), it still
throws the "Context validation error for tag cfselect. The tag must be
nested inside a cfform tag." error.

not sure if it is a bug or expected behaviour - will have to check the
documentation...

for a work-around, you can have the actual <cfselect> tag inside
<cfform> and just return <option> tags from you cfc...

Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com/
jim1234Author
Inspiring
December 5, 2008
Thanks for the confirmation.

When I looked at the documentation, it didn't say anything about not being able to put the cf type form elements in functions, but maybe you'll find something. It just seems like the cf form elements should behave the same in this case as the regular html form elements.

It looks as if ColdFusion is not waiting for the final page rendering to validate the context of the tags and rather is just taking what it can see within the function.

I'll see if anyone else finds anything, and if not, I'll report it as a bug, even though it doesn't help me right now.

Thanks,
Jim
Inspiring
December 4, 2008
I can't see any reason why it wouldn't work. What happens if you put that cfcontent stuff inside your .cfm file? This would be for troubleshooting purposes only.
jim1234Author
Inspiring
December 4, 2008
Thanks for the reply. Yes, that works. So it looks like it has to do with the function call.

After some further investigation, I found out that a regular <select> works, it's just the <cfselect> that does not. ColdFusion wants it directly within the <cfform> tag.
Inspiring
December 5, 2008
quote:

Originally posted by: jim1234
Thanks for the reply. Yes, that works. So it looks like it has to do with the function call.

After some further investigation, I found out that a regular <select> works, it's just the <cfselect> that does not. ColdFusion wants it directly within the <cfform> tag.

Something does not make sense. If the cfsavecontent worked in the cfm file, there is no reason for it not to work in a cfc.

If you want to persue this, the next troubleshooting step is to copy the function to your cfm file and see if it works. I suspect that you are trying something simple as a proof of concept.