Skip to main content
Participant
October 27, 2007
Question

<cfif isdefined is not working

  • October 27, 2007
  • 4 replies
  • 621 views
I am using cold fusion 8 to develop forms. I need to check if user has entered a value of type "text". If not then a message has to Cfoutput. i am using the following code:

<cfif not isdefined("form.question")>
<cfoutput> Enter question
</cfoutput>

</cfif>


The problem is even f nothing is entered in the Question Field, the message Enter question is not being displayed. Its as is the <cfif not isdefined("form.question")> is not at all being recognised.
I also tried <cfif not isdefined("form.question_now")>

Please help.
This topic has been closed for replies.

4 replies

BKBK
Community Expert
Community Expert
October 30, 2007
BKBK:
> > That is why you would get the same response if you used <cfif not isdefined("form.anyArbitraryFormFieldName")>.

Kronin555:
Not quite. Checkboxes aren't submitted with the form unless they're checked.

Thanks, Kronin555. I did overlook the checkbox. I have included the correction in my post.




BKBK
Community Expert
Community Expert
October 29, 2007
The problem is even [if] nothing is entered in the Question Field, the message Enter question is not being displayed. Its as [if] the <cfif not isdefined("form.question")> is not at all being recognised.

<cfif not isdefined("form.question")> actually answers the question whether or not the form has been submitted. That is why you would get the same response if you used <cfif not isdefined("form.anyArbitraryFormFieldName")>. If you wish to check for blank fields, then do something like

<cfif not isdefined("form.question") or trim(form.question) is "">


added edit: Read form.anyArbitraryTextFormFieldName in place of form.anyArbitraryFormFieldName





Participating Frequently
October 30, 2007
> That is why you would get the same response if you used <cfif not isdefined("form.anyArbitraryFormFieldName")>.

Not quite. Checkboxes aren't submitted with the form unless they're checked.

The easiest thing to do, in my opinion, is to <cfparam> all your form fields at the top of your action page. Then you don't have to worry about checking for isDefined() at all, and you can just check the data (length, equality, etc).

If you put <cfparam name="FORM.question" default=""> at the top of your action page, then the rest of the page already knows that it's defined and can just check for length if you want to make sure they entered something.
October 29, 2007
Hi Swap,

As Azadi mentionned, text input fields are always defined... if they are defined in the submitted form. In short, never assume...

Attached code is a short example of an empty field check. You can play with variables to define flags, error messages, loop...

Hope this helps,
Chris
Inspiring
October 28, 2007
text input fields are ALWAYS defined - i.e. they exist in the form scope
even if nothing has been entered into them. in this they are different
from, say, radio buttons and check boxes.
what you need to be checking for instead is the LENGTH of the field's value.

---
Azadi Saryev
Sabai-dee.com
http://www.sabai-dee.com