CFINPUT required="yes" not working with CFLOOP - coldfusion

When I click Submit button (no data was entered) the text boxes were empty and it still goes to the next page without popping up any messsage. It seems the cfinput required="yes" does not work the cfloop. I thought it should work even with the cfloop. I could not find what was wrong. How can I make it work? any info is greatly appreciated. Thank you.
<cfform name="theForm" action="nextPage.cfm" method="post">
<table>
<tr>
<td><cfinput type="text" name="A" size="50" required="yes message="please enter your text"></td>
</tr>
<td><cfinput type="text" name="B" size="50" required="yes message="please enter your text"></td>
</tr>
<cfloop from=1 to=5 index=i>
</tr>
<td><cfinput type="text" name="C" size="50" required="yes message="please enter your text"></td>
</tr>
</tr>
<td><cfinput type="text" name="D" size="50" required="yes message="please enter your text"></td>
</tr>
</cfloop>
<tr>
<td><cfinput type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</cfform>

For this to work, you need to have unique NAMEs for each input field. Right now, you're creating a bunch of inputs named either "C" or "D". If any one "C" is filled in, then no other field named "C" needs to be filled in. Create unique names using your index variable and the validation should work as you intend.
<cfloop from=1 to=5 index=i>
</tr>
<td><cfinput type="text" name="C_#i#" size="50" required="yes message="please enter your text"></td>
</tr>
</tr>
<td><cfinput type="text" name="D_#i#" size="50" required="yes message="please enter your text"></td>
</tr>
</cfloop>

There is a syntax error in the cfinput tag
It should be
<cfinput type="text" name="D" size="50" required="yes" message="please enter your text">
There is a missing quote after yes

In addition to what was said you should use something else and not cfform if you want anything more than basic validation. The cfform stuff is really legacy and there are much better options these days.

Related

Passing form input using Coldfusion to external URL

I am trying to pass a form input to an external URL to be used somewhere else and perform a certain task. The issue I am having is actually getting the value and passing it. I either get a LotNumber is undefined or an error executing a Cfquery. I tried using CFPARAMhowever I think I misunderstand what cfparam does. Hopefully this is something simple.
Here is some code:
<table border="1" cellpadding="3" cellspacing="0" rules="GROUPS" frame="BOX" width="100%" bordercolor="#C0C0C0" bgcolor="#FFFFFF">
<thead>
<tr height="28">
<td colspan="4"><b>Outstanding Shipping</b></td>
<CFOUTPUT>
<cfparam name="Show_SampleLogSheet.Passed_LotNumber" default="" />
<td align="left" colspan="1">
<input class="frm3" type="text" id="Outstanding_Passed_LotNumber" size="3" maxlength="6" tabindex="25" style="background-color: ##838383;border:1px solid ##000000; color:white">
<form name="Show_SampleLogSheet" class="frm" action="/Buying/Shipping_Advice/Index.cfm?Passed_CustomerID=#Passed_CustomerID#&Passed_ShippingAdviceID=#Get_ShippingAdvice.ShippingAdviceID#&Passed_Lot_Number=#Show_SampleLogSheet.Passed_LotNumber#&Passed_Activate=1" method="post" style="display: inline">
<input type="hidden" name="Passed_CustomerID" value="#Passed_CustomerID#">
<input class="frm3" type="text" name="Passed_LotNumber" value="#Show_SampleLogSheet.Passed_LotNumber#" size="3" maxlength="6" tabindex="25">
</form>
</td>
</CFOUTPUT>
I really appreciate any help.
Thank you
For names are client side. ColdFusion does not need to name them at all. (Code has been somewhat similified
<cfparam name="Passed_LotNumber" default="" />
I don't know what this field is good for. It is not within the form tag, so it is not going to get pushed over on submit.
<input class="frm3" type="text" id="Outstanding_Passed_LotNumber" size="3" maxlength="6" tabindex="25" style="background-color: ##838383;border:1px solid ##000000; color:white">
Real form starts here. Note that passed_LotNumber does not need anything
<form name="Show_SampleLogSheet" class="frm" action="/Buying/Shipping_Advice/Index.cfm?Passed_CustomerID=#Passed_CustomerID#&Passed_ShippingAdviceID=#Get_ShippingAdvice.ShippingAdviceID#&Passed_Lot_Number=#Passed_LotNumber#&Passed_Activate=1" method="post" style="display: inline">
<input type="hidden" name="Passed_CustomerID" value="#Passed_CustomerID#">
<input class="frm3" type="text" name="Passed_LotNumber" value="#Passed_LotNumber#" size="3" maxlength="6" tabindex="25">
Turns out it was some the wrong input name. Here is the fixed code:
<td align="left" colspan="1">
<input class="frm3" type="text" id="Outstanding_Passed_LotNumber" size="3" maxlength="6" tabindex="25" style="background-color: ##838383;border:1px solid ##000000; color:white">
<form name="Show_SampleLogSheet" class="frm" action="/Buying/Shipping_Advice/Index.cfm" method="post" style="display: inline">
<input type="hidden" name="Passed_CustomerID" value="#Passed_CustomerID#">
<input class="frm3" type="text" name="Passed_Lot_Number" size="3" maxlength="6" tabindex="25">
</form>
</td>
There was a parameter that was hidden somewhere else named Passed_Lot_Number instead of Passed_LotNumber. I apologize, this is some super crap code and it's super old thus all of these dumb headaches. Thank you everyone

Coldfusion Server Side Validation Sometimes Required sometimes not depending on radio buttons

I am trying to get a better understanding of server side validation. I have been writing a lot of client side validation using JavaScript but did not realize if the user just turns JavaScript off the whole app does not validate any field. This is what I am looking into doing for ColdFusion Server side validation.
What I am wondering is how would you toggle on and off making a field required or not based on lets say a radio buttons yes or no. Say you have a radio button the if yes is chosen it makes another input required but if no is chosen it makes another field required. I was just wondering how you would do something like that with the _cf format of toggling on and off. Would you just create if statements on hidden fields to do so or something?
I have been researching it a lot and just was looking for some input on how people are achieving things like this because it just seems like you can do so much through client side but then its all pointless because they can just turn it off.
<table>
<tr>
<td>Date:</td>
<td><input type="text" name="date" size="20"></td>
<strong class="delete me and add my line number to the highlight in my pre tag">
<input type="hidden" name="date_cfformrequired" value="You must enter a date.">
<input type="hidden" name="date_cfformdate" value="The date you entered is not a valid date."></strong>
</tr>
<tr>
<td>Distance:</td>
<td><input type="text" name="distance" size="20"></td>
<strong class="delete me and add my line number to the highlight in my pre tag">
<input type="hidden" name="distance_cfformrequired" value="You must enter a distance.">
<input type="hidden" name="distance_cfformfloat" value="The distance you entered is not a valid number."></strong>
</tr>
<tr>
<td>Time:</td>
<td><input type="text" name="time" size="20"></td>
<strong class="delete me and add my line number to the highlight in my pre tag">
<input type="hidden" name="time_cfformrequired" value="You must enter a time.">
<input type="hidden" name="time_cfformregex" value="^\d{1,3}:\d{0,2}$"></strong>
</tr>
<tr>
<td>Comments:</td>
<td><input type="text" name="comments" size="50"></td>
<strong class="delete me and add my line number to the highlight in my pre tag">
<input type="hidden" name="comments_cfformrequired" value="You must enter a comment.">
<input type="hidden" name="comments_cfformmaxlength" value="50"></strong>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" name="Add Entry">
</td>
</tr>
</table>
Your understanding of the difference between client and server side validation is correct. Consider that client side validation can be chalked up to a feature. Rather than the user entering information, submitting the form, and then being told something was incorrect, Javascript validation can guide them, but because it can be disabled, it's not good to rely on.
Server side validation always happens as dictated.
The code is pretty easy
As an example - HTML
Name:
<input type="text" name="myname">
Were you referred by a current user?
<input type="radio" name="Referred" value="0" checked> No
<input type="radio" name="Referred" value="1"> Yes
Who referred you?
<input type="text" name="ref_user">
Form processing
<cfset Err = {Messages = []}> // creates a struct named Err with an array names Messages.
<cfif len(trim(form.myname)) eq 0>
<cfset ArrayAppend(Err.Messages,"Please enter your name!">
</cfif>
<cfif form.Referred eq 1 and len(trim(form.ref_user)) eq 0>
<cfset ArrayAppend(Err.Messages,"You said someone referred you, who was it?">
</cfif>
<cfif ArrayLen(err.messages) eq 0>
...no errors, we can do form processing...
</cfif>
And then, when outputting your errors, if they exist
<cfif ArrayLen(err.messages) gt 0>
Sorry, an error occurred.<br>
<cfoutput>
<cfloop array="#err.messages#" index="cError">
- #cError#.<br>
</cfloop>
</cfoutput>
</cfif>
On the first line of the last code snippet, you'll see gt 0 at the end of the line. This is not necessary and most people will leave it off. You seem familiar with javascript, cf works the same way in that in that you might say
if (Err.Messages.length) {
... show
}
In cf, you can do similar, <cfif ArrayLen(Err.Messages)>.
Remember to either cfparam your variables or check for their existence in your form processing, like this..
<cfif not StructKeyExists(form, "Referred") or (form.Referred eq 1 and len(trim(form.ref_user)) eq 0)>
<cfset ArrayAppend(Err.Messages,"You said someone referred you, who was it?">
</cfif>

Cfmail and cftextarea not retaining line breaks

I am working on a site that is having a problem with one of its mail forms. When a user enters text into a CFTEXTAREA, it doesn't retain any of the formatting (pressing 'enter' doesn't result in a line break). Not when its stored as a variable or emailed by a <CFMAIL type="HTML" ..>).
A site admin claims that it used to work fine. Any tips?
<cfprocessingdirective suppresswhitespace="No">
<cfmail to="OMITTED" from="OMITTED" subject="#subject#" type="html">
#message#
</cfmail>
</cfprocessingdirective>
<cfform name="mail" action="./mailinglist.cfm?sendMessage=true"
format="#type#"
height="400">
<cfformgroup type="vertical">
<table width="99%" cellpadding="3" cellspacing="0">
<tr>
<td class="form_label">Subject: </td>
<td class="form_field">
<cfinput class="textbox" required="yes"
message="Please fill out the subject heading."
style="width:200px;"
type="text"
name="subject"
label="Subject:" width="200">
</td>
</tr>
<tr>
<td class="form_label">Message: </td>
<td class="form_field">
<cftextarea class="textbox" required="yes"
id="message"
name="message"
message="Please fill out the message body of the form..."
style="width:300px; height:150px;"
width="350" height="250"
label="Message:"
wraptext="72" ></cftextarea>
</td>
</tr>
<tr>
<td class="form_label"></td>
<td class="form_field" align="left" valign="top">
<cfinput class="textbox" disabled value="yes"
type="checkbox"
name="announcement"
label="Post as Announcement?">
Post as Announcement?
</td>
</tr>
</cfformgroup>
To preserve line breaks from a textarea in an HTML-formatted email, you must replace the line breaks with <br> tags:
reReplace(message, '\n', '<br />', 'ALL')
If you don't have additional HTML-formatted content in your email message, you can simply remove the type="HTML" attribute from the CFMAIL tag, and the text from your text area will appear as it was entered with line breaks intact.
I'd use #imthepitts answer but just to add some additional options you can also use
replace(message, chr(10), '<br />', 'ALL')
or
<cfmail type="html">
<pre>
#message#
</pre>
</cfmail>
Note: Markup, all left aligned intentionally to prevent unwanted indentation in the email.
Try using a rich textarea.
<cftextarea name ="message" richtext="true"
other attributes
>
</cftextarea>
Inside your CFmail, you can use #paragraphformat(message)# and that will keep your line breaks.

ColdFusion - Using a cfloop with multiple fields and multiple submit buttons

I'm looking to pass form values in a cfform to a PDF using cfpdfform. Here's my little test page that loops through 50 records to pull the first and last name. I'm trying to just pull those into the pdf fields. Currently it puts in all 50 of the first names into the firstname field and all of the lastnames into the lastname field of the pdf. I'm not married to the submit button, but what are better options?
In my final iteration of this I'll be pulling in about 100 fields.
--Form--
<cfform name="autopdf" method="POST" action="automated_pdf_submit.cfm" enctype="multipart/form-data">
<h1>Select a state to insert into a PDF form</h1>
<div class="center">
<select name="pdfselect" id="pdfselect">
<option value="" selected>--Select State--</option>
<option value="FROI_NY.pdf">New York</option>
<option value="FROI_PA.pdf">Pennsylvania</option>
</select>
<cfinput type="hidden" name="statevalidate" onValidate="yourFunction"
message="YOU MUST SELECT A STATE TO CONTINUE!">
</div>
<table align="center" style="width:400px">
<tr>
<th></th>
<th>First Name</th>
<th>Last Name</th>
<th>Export to PDF</th>
</tr>
<cfoutput>
<cfloop query="#qryPersons#" startrow="1" endrow="50" >
<tr class="#IIf(CurrentRow Mod 2, DE('rowOdd'), DE('rowEven'))#" onmouseover="this.className='rowHighlight'"
<cfif CurrentRow Mod 2>onmouseout="this.className='rowOdd'"
<cfelse>onmouseout="this.className='rowEven'"</cfif>>
<td>#qryPersons.CurrentRow#</td>
<td>#qryPersons.LastName#</td>
<input type="hidden" name="FirstName" value="#qryPersons.LastName#">
<td>#qryPersons.FirstName#</td>
<input type="hidden" name="LastName" value="#qryPersons.FirstName#">
<td style="width:50px"><input type="submit" value="Create PDF"</td>
</tr>
</cfloop>
</cfoutput>
</table>
</cfform>
--Action--
<cfpdfform action="populate" source="forms\#form.pdfselect#">
<cfpdfformparam name="FirstName" value="#form.FirstName#">
<cfpdfformparam name="LastName" value="#form.LastName#">
</cfpdfform>
Your form fields are all named FirstName and LastName you need to make those unique
<cfloop query="#qryPersons#" startrow="1" endrow="50" >
<tr class="#IIf(CurrentRow Mod 2, DE('rowOdd'), DE('rowEven'))#" onmouseover="this.className='rowHighlight'"
<cfif CurrentRow Mod 2>onmouseout="this.className='rowOdd'"
<cfelse>onmouseout="this.className='rowEven'"</cfif>>
<td>#qryPersons.CurrentRow#</td>
<td>#qryPersons.LastName#</td>
<input type="hidden" name="FirstName#qryPersons.currentrow#" value="#qryPersons.LastName#">
<td>#qryPersons.FirstName#</td>
<input type="hidden" name="LastName#qryPersons.currentrow#" value="#qryPersons.FirstName#">
<td style="width:50px"><input type="submit" value="Create PDF"</td>
</tr>
</cfloop>
I've never used cfpdfform before, but this syntax should work. You may need to dynamically name the name attribute below as well
<cfpdfform action="populate" source="forms\#form.pdfselect#">
<cfloop from="1" to="50" index="i">
<cfpdfformparam name="FirstName" value="#form['FirstName'&i]#">
<cfpdfformparam name="LastName" value="#form['LastName'&i]#">
</cfloop>
</cfpdfform>

ColdFusion 9 ReReplace nth occurence of HTML tag

I have the following HTML stored in a variable in ColdFusion 9. I need to insert a new table row after the 4th </tr>. i.e. before the Submit button.
<form name="form1" id="form1" action="" method="post">
<table>
<tr style="visibility:hidden;display:none;"><td> <input type="hidden" id="ref1" name="ref1" value="1" > </td></tr>
<tr style="visibility:hidden;display:none;"><td> <input type="hidden" id="ref2" name="ref2" value="2" > </td></tr>
<tr>
<th style="text-align:left;">Name * </th>
<td><input type="text" name="foo" id="foo" size="30" maxlength="50" value=""></td>
</tr>
<tr>
<th title="Please enter plain text or HTML." style="cursor:help;text-align:left;">Comment * </th>
<td><textarea name="bar" id="bar" cols="40" rows="10" ></textarea></td>
</tr>
<tr>
<th colspan="1"></th>
<td>
<input style="width:80px" type="submit" value="Submit">
<input style="width:80px" type="button" value="Cancel">
</td>
</tr>
</table>
ReReplace seems like the way to go, but I'm having trouble getting the regexp right. Another option would be to split the string and rebuild it with my new HTML in the middle. Any suggestions would be appreciated.
Regex is the wrong tool for this - you want a HTML parser.
Here's how you can do it with JSoup:
<cfsavecontent variable="InputHtml">
[insert code from question]
</cfsavecontent>
<cfsavecontent variable="NewRow">
<tr><th>whatever</th><td>stuff</td></tr>
</cfsavecontent>
<!--- Read "Creating Objects From Jar Files" --->
<cfset jsoup = createObject('java','org.jsoup.Jsoup') />
<cfset HtmlDom = jsoup.parse(InputHtml) />
<cfset HtmlDom.select('tr:eq(4)').after( NewRow ) />
<cfoutput>
<pre>#XmlFormat(HtmlDom.body().html())#</pre>
</cfoutput>
You can see details of what selectors are supported in the JSoup Selector API
If you don't know/care how many lines are in the table, you can do...
HtmlDom.select('table>tbody').append( NewRow )
...to just add the new row at the end.
Creating Objects From Jar Files
The above code most likely wont work instantly if you copy and paste it, because your server doesn't know about JSoup - you need to download the Jar file and put it in a sensible location.
For CF9, you need to copy the jsoup-1.6.3.jar into your {coldfusion}/lib directory then restart the server.
For CF10, you can use this.JavaSettings in your Application.cfc (as described here) to place it in a different location.
For Railo and OpenBD, you can specify the location of the JAR file as a third argument, e.g:
<cfset jsoup = createObject('java','org.jsoup.Jsoup','lib/jsoup-1.6.3.jar') />
I recommend doing this with jQuery:
​$(document).ready(function(){
$($('form tr')[3]).after('<tr><td>row</tr></tr>');
});​
Much easier.