How do I make conditional values in <cfinput> fields?

Posted on 2010-01-01
Last Modified: 2013-12-24
How can I make conditional values using <cfinput> fields?
I normally use a regular input tag with a <cfif> statement inside it, like so:

<input type="text" name="productName" <cfif IsDefined("FORM.productName")> value="#FORM.productName#"</cfif> />

However, when using a <cfinput> tag, you can't nest a <cfif> inside it. :(

I need to use <cfinput> so that I can have form validation (required="yes" validateAt="onServer", etc.), but I need to make the values conditional.

What are my options?
Question by:chaseivey
    LVL 27

    Accepted Solution

    use iif() function []:

    <cfinput type="text" name="productName" value="#iif(structkeyexists(FORM, 'productName'), de(form.productname), de(''))#"  />

    another option is to put <cfinput> inside a cfif block:
    <cfif structkeyexists(form, 'productname')>
    <cfinput type="text" name="productName" value="#form.productname#"  />
    <cfinput type="text" name="productName" value=""  />

    LVL 51

    Expert Comment

    A third option is to set a default for the form fields. Then neither IIF or cfif would be needed:

    <cfparam name="form.productName" default="">

    <cfinput type="text" name="productName" value="#FORM.productName#" />

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
    Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now