Changes between Initial Version and Version 1 of QuickStart/5:ValidatingaForm

Show
Ignore:
Timestamp:
04/15/09 19:43:30 (17 years ago)
Author:
trac (IP: 127.0.0.1)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • QuickStart/5:ValidatingaForm

    v1 v1  
     1= Quickstart 5: Validating a Form = 
     2 
     3Our translation form doesn't require the user to enter a value. Our application doesn't fail, but it'd be nice to server-side validate the form. 
     4 
     5(Actually, it's not just nice, it's something you should do for every form, every time.) 
     6 
     7Using the translateForm function, it's easy to check if the form as been filled out. However, how do we alert the user? How do we take them back to the form, instead of showing the result? 
     8 
     9The <event-handler> tag has a third child tag, <results>, that's custom made for things like this. 
     10 
     11Adding a <results> and <result> tag to our translationFormAction event handler, we end up with the following xml: 
     12 
     13{{{ 
     14<event-handler name="translationFormAction"> 
     15 
     16<broadcasts> 
     17 
     18<message name="NeedTranslation" /> 
     19 
     20</broadcasts> 
     21 
     22<views> 
     23 
     24<include name="body" template="dspPhrase.cfm"> 
     25 
     26<value name="xe.translationForm" value="translationForm" /> 
     27 
     28</include> 
     29 
     30</views> 
     31 
     32<results> 
     33 
     34<result name="ValidationError" do="translationForm" redirect="true" /> 
     35 
     36</results> 
     37 
     38</event-handler> 
     39}}} 
     40 
     41What does that <result> tag mean? It means that if I somehow state that the event has a validation error, immediately stop processing the event and redirect to the translationForm event. 
     42 
     43So how do we indicate that there's a validation error? We test a condition in our controller, and if necessary, add the result to arguments.event: 
     44 
     45Doing this, the !TranslatePhrase function now reads: 
     46 
     47{{{ 
     48<cffunction name="TranslatePhrase" access="public" returntype="void" output="false"> 
     49 
     50<cfargument name="event" type="any"> 
     51 
     52<cfset var translator = createObject("component", "translator.model.PigLatinTranslator").init("aeiou") /> 
     53 
     54<cfset var phrase = arguments.event.getValue("phrase") /> 
     55 
     56<cfset var result = translator.translate(phrase) /> 
     57 
     58<cfif not len(trim(phrase))> 
     59 
     60<cfset arguments.event.addResult("ValidationError") /> 
     61 
     62</cfif> 
     63 
     64<cfset arguments.event.trace("TranslatePhrase Results", result) /> 
     65 
     66<cfset arguments.event.setValue("translatedPhrase", result) /> 
     67 
     68</cffunction> 
     69}}} 
     70 
     71Try the submitting the form without filling in a phrase. You'll wind up right back at the form. That's OK, but we need to provide an error message. 
     72 
     73Remember, we can use controller functions to add values to the viewstate that view templates can display. Let's first add an error message to !TranslatePhrase: 
     74 
     75{{{ 
     76<cffunction name="TranslatePhrase" access="public" returntype="void" output="false"> 
     77 
     78<cfargument name="event" type="any"> 
     79 
     80<cfset var translator = createObject("component", "translator.model.PigLatinTranslator").init("aeiou") /> 
     81 
     82<cfset var phrase = arguments.event.getValue("phrase") /> 
     83 
     84<cfset var result = translator.translate(phrase) /> 
     85 
     86<cfif not len(trim(phrase))> 
     87 
     88<cfset arguments.event.setValue("phraseError", "Please enter a phrase to translate.") /> 
     89 
     90<cfset arguments.event.addResult("ValidationError") /> 
     91 
     92</cfif> 
     93 
     94<cfset arguments.event.trace("TranslatePhrase Results", result) /> 
     95 
     96<cfset arguments.event.setValue("translatedPhrase", result) /> 
     97 
     98</cffunction> 
     99}}} 
     100 
     101Then, change frmPhrase.cfm to display the phrase error, if one has been set: 
     102 
     103{{{ 
     104<cfset submit = viewstate.getValue("myself") & viewstate.getValue("xe.translate") /> 
     105 
     106<cfform action="#submit#"> 
     107 
     108<cfinput type="text" name="phrase" required="false" value="#viewstate.getValue("phrase")#" /> 
     109 
     110<input type="submit" value="Ok" /> 
     111 
     112<cfif viewstate.exists("phraseError")> 
     113 
     114<cfoutput><p><font color="red">#viewstate.getValue("phraseError")#</font></p></cfoutput> 
     115 
     116</cfif> 
     117 
     118</cfform>