Home > ColdFusion, Web Application Development > Learning CFWheels – simple form

Learning CFWheels – simple form

First, thanks to the very nice people who responded to my question about the select() form helper.  Using the feedback I received I was able to get my states drop down populated.  I simply added the following to my user controller’s add() method:

<cfset states = model("state").findAll()>

Then in my add.cfm in the user view, I added the select() like so:

<div>#select(objectName="user", property="state", label="State: ", valueField="state_abbr", options=states, includeBlank="true" )#</div>

Now my form appears and my drop down list is populated. I set the includeBlank argument to true so that a blank value is inserted for the first item in the list.  I suppose in my form validation, I could do a check for a blank entry if I decide to make this a required entry, but that will come later. My form now appears as so:

Add user form

Add user form

This then got me thinking of what comes next.  We use the same form to edit users.  In the edit form, we populate the form with the selected record from the DB.  That begs the question, “How do we get the correct value to be selected in the states drop down?”  Normally I would use the following code:

<select name="states">
     <cfloop query="qStates>
          <option value="#state_abbr#" <cfif states EQ states>selected="selected"</cfif></option>
     </cfloop>
</select>

I guess they already thought about this at CFWheels.  I just added the same findAll call from the add() method to the edit() method of the user controller.  Somewhere behind the scenes, CFWheels seems to have figured out what I was trying to do because when I selected a record for editing, I see this:

Edit user form

Edit user form

Some quick data validation in the DB confirms that the data was entered and retrieved as expected.

That’s all for today.  Even though this was extremely simple, I’m happy to be making progress in learning CFWheels.  I’ll try to continue creating a form and processing the data next.  Again a big thank you to the nice people who responded with tips.  You do the CF community proud.

Advertisements
  1. Bernie
    February 8, 2012 at 12:02 pm

    Hello, this isn’t working for me. I’ve tried variations with and without the selected and value_field options. Any ideas?

    controller…

    view/census/edit.cfm
    #select( label=”Program”, objectName=”census”, property=”programID”, options=programs, selected=”census.program_no”, value_field=”program_no”, text_field=”program_name”, includeBlank=”–Select One–“)#

    • kwbarrett
      February 8, 2012 at 12:37 pm

      The first thing that jumps out at me is your syntax for attributes in the select() tag. valueField and textField are proper. Aside from that are you getting an error message? What does the error say? In your controller, have you defined the programs object in your controller?

      Your controller should look something like: <cfset census = model("model name“).new()/> <cfset programs = model("model name“).findAll()/>.

      Your view should look something like: #select(label=”Program”, objectName=”your object name“, property=”programID”, options=programs, valueField=”program_no” textField=”program_name”, includeBlank=”-select one-“)#.

      • Bernie
        February 8, 2012 at 12:47 pm

        no error it just doesn’t do it. Sorry my text is getting cut off. My controller code is

        The pds_program model uses a second datasource. Does that matter?

      • Bernie
        February 8, 2012 at 3:52 pm

        It’s working! I changed the column names to be identical in both tables involved. I also added select=”programID, programName” to the findAll() in the control file . I removed the quotes around the column name in selected=columname from the view, #select( label=”Program”, objectName=”census”, property=”programID”, options=programs, selected=census.programID, value_field=”programID”, text_field=”programName”, includeBlank=”–Select One–“)#. The pds_program table has both an ID field (the PK) and programID field. I noticed in the generated source code that it was using the ID as the select value even though I specified it to use the programID. Since these IDs do not match the programID nothing was pre-selected. I don’t know what I did but this eventually resolved itself.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: