Skip to main content
BreakawayPaul
Inspiring
May 8, 2013
Answered

Form question

  • May 8, 2013
  • 2 replies
  • 1120 views

Every two years I have to create a web form for people to submit online nominations for transportation awards.  Every time I create it, I think I'll be able to reuse it the next time, but too much ends up changing (the questions, security requirements, etc) that I end up having to do more than half of it over.

The form has JS validation for all fields for data type, max length, etc, but it also has serverside validation.  If you submit the form with JS disabled and there's an error, the form reloads (populated with the data you just entered) and displays error messages telling you what's wrong, so you can edit the data and resubmit.

It's a large form, so I don't want to have more than one copy of it, so my single copy has to:

1) Be a new (blank) form.

2) Be the form that gets populate from the database when people want to edit their existing nomination.

3) Retain the information that someone just entered when there's an error.

The way I've been doing this is to <cfparam> all the FORM data then do this for the values of all the inputs:

value="#iif(FORM.nominator_org neq "",DE(FORM.fieldname),DE(query.fieldname))#"

If the FORM variable has a value, use it, otherwise use the value from the query.  Needless to say, this is a bit clunky, so I was wondering if there was a cleaner way.  There are over 40 fields, so it ends up being a lot of code!

    This topic has been closed for replies.
    Correct answer Reed_Powell-ttnmOb

    When you say that you are CFPARAming all the FORM data, what exactly are you using for the default values?  Seems like all you need to do is to do the query to get database values, and then a string of CFPARAMs for each FORM field, where you set the default value to the corresponding value from the query.  Then just use the FORM value in the VALUE clause of the CFINPUT.

    Was also not clear about how you are doing the client-side validation with JS - are you using the built-in CFINPUT features, or your own JS?  if you are using your own JS, then don't use CFINPUT, just INPUT.  That way you can have the CFIF tags inside of the INPUT tag.  If you actually need to use the CFINPUT tags, then you can avoid the IFF/DE hassle by putting the CFIF tests before the CFINPUT tag, set a temp var to the appropriate value, and then use the temp var in the VALUE for the CFINPUT.

    hth

    -reed

    2 replies

    Reed_Powell-ttnmObCorrect answer
    Inspiring
    May 8, 2013

    When you say that you are CFPARAming all the FORM data, what exactly are you using for the default values?  Seems like all you need to do is to do the query to get database values, and then a string of CFPARAMs for each FORM field, where you set the default value to the corresponding value from the query.  Then just use the FORM value in the VALUE clause of the CFINPUT.

    Was also not clear about how you are doing the client-side validation with JS - are you using the built-in CFINPUT features, or your own JS?  if you are using your own JS, then don't use CFINPUT, just INPUT.  That way you can have the CFIF tags inside of the INPUT tag.  If you actually need to use the CFINPUT tags, then you can avoid the IFF/DE hassle by putting the CFIF tests before the CFINPUT tag, set a temp var to the appropriate value, and then use the temp var in the VALUE for the CFINPUT.

    hth

    -reed

    BreakawayPaul
    Inspiring
    May 8, 2013

    Thanks Reed.

    At the top of the page I have these:

    <cfparam name="FORM.fieldname" default=""> (for each field)

    And I'm using the built-in <cfinput> validation, which is why I'm using the <cfinput>.  Well, that and I like <cfselect>.

    If I was better with JS, I'd probably roll my own, but the <cfinput> validation has worked fine for the last three versions of this form, with no complaints from users.

    So you're saying that instead of doing the params that way, I could do the query first?  Then do this:

    <cfparam name="FORM.fiieldname" default="queryname.columname"> ?

    Inspiring
    May 8, 2013

    Yes, but be sure to put hash marks around the query.queryname:

    <cfparam name="form.lastname" default="#queryname.lastname#">

    Participating Frequently
    May 8, 2013

    IIF and DE aren't widely used these days in CF. 

    Are the form fieldnames the same as the query columns?  If so you could do something like:

    IF using the query

      set stuData = yourQueryName

    ELSE

      set stuData = form

    /IF

    Then refer to it like:

    <input type="text" value="<cfif len(stuData[fieldname])>#stuData[fieldname]#</cfif>" />

    BreakawayPaul
    Inspiring
    May 8, 2013

    If I don't use DE with the IIF, I get an error.  The reason I used the IIF is because I'm using <cfform> and<cfinput> (horrible, I know) so I can't have a <cfif> in there.

    But yes, the field names are the same as the column names.