ColdFusion Validation Limit

Hi,
This might sound like a rookie question but here goes.  I do have a solution but it's ugly.

Most of my programs validate all the forms fields server side and display any error encountered.  I just go through a series of CFIF blocks testing each form field.  If invalid, I concatenate an error message to a variable of error messsages found.  See sample source for an example.

In this case, I want to stop after 2 errors encountered and only display the first 2 errors encountered.

The only way I can think of doing this is to have a error counter and wrap an CFIF statement around each test to either skip it or skip the concatenation of the error messages if the maxerror counter has been reached.

Is their another way to do this?

Thanks,
hefterr




<cfif  Isdefined("form.save")>
  <cfif len(trim(form.firstName)) eq 0>
    <cfset errmsg = errmsg & errormsg1>
  </cfif>
  
  <cfif len(trim(form.lastName)) eq 0>
    <cfset errmsg = errmsg & errormsg2>
  </cfif>
  etc......
</cfif

<cfif errmsg neq "">
display page with errors
<cfelse>
Update tables
</cfif

Open in new window

LVL 1
hefterrAsked:
Who is Participating?
 
_agx_Connect With a Mentor Commented:
What I like to do is add errors to an array. Then I can control what happends and how may are displayed. Since it's an array I can easily convert it to a string with arrayToList() , etc.. Note, I wouldn't actually stop validation after 2 errors. Just control how many errors are displayed.

<cfset errors = []>
...
   <cfif len(trim(form.firstName)) eq 0>
       <cfset arrayAppend(errors, "some message ...")>
  </cfif>
  <cfif len(trim(form.lastName)) eq 0>
       <cfset arrayAppend(errors, "other message...")>
  </cfif>
....

<cfif arrayLen(errors)>
    <!--- display the 1st x messages or loop to display them all --->
   <cfoutput>
    #errors[1]#
    #errors[2]#</cfoutput>
</cfif>
0
 
cfEngineersConnect With a Mentor Commented:

<cfif  Isdefined("form.save")>
  <cfset errorcount = 0>
  <cfif len(trim(form.firstName)) eq 0 and errorcount lt 2>
    <cfset errmsg = errmsg & errormsg1>
	<cfset errorcount = errorcount + 1>
  </cfif>
  
  <cfif len(trim(form.lastName)) eq 0 and errorcount lt 2>
    <cfset errmsg = errmsg & errormsg2>
	<cfset errorcount = errorcount + 1>
  </cfif>
  etc......
</cfif

<cfif errmsg neq "">
display page with errors
<cfelse>
Update tables
</cfif>

Open in new window

0
 
_agx_Commented:
Note, I wouldn't actually stop validation after 2 errors. Just control how many errors are displayed.

Not unless there's a real compelling reason.  Skipping a few lines of server side validation doesn't usually buy much.  So unless the process is very time consuming, I prefer to be thorough and run it all. Then limit the display/output.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
hefterrAuthor Commented:
Thanks to both for you ideas.  I didn't think of either of them.
@agx:  Your solution is a bit more complex but it has the  advantage for debugging as you can dump the table and see all the messages that were "collected".

@cfEngineers:  I like the simplicity of your solution.  For some reason I just didn't think of that.

It's great getting other eyes on a problem.  I work at home for a small company and this forum is terrific for that!
0
 
_agx_Commented:
lol.  I find the array much simpler because there are no extra conditions and don't have to worry about manually incrementing a variable inside every validation check.  But there's nothing wrong with it.  A legacy apps I work with does the same thing, and it works just as well ;-)
0
 
hefterrAuthor Commented:
Hmmmmmm.  I guess you are correct as your arrayAppend statement replaces my string append statement.

But you do need a extra loop at the end to output the messages you want to display (not a big deal).

Either way - it is very elegant and I always like to see other ways to do things.  Maybe I'll give it a try.

hefterr
0
 
hefterrAuthor Commented:
@agx:
A legacy apps I work with does the same thing, and it works just as well ;-)

I think  I've become a legacy :)
0
 
_agx_Commented:
But you do need a extra loop at the end to output the messages you want to display (not a big deal).

True, but that's only 3 lines of code ;-) The string append / counter method has double the code .. for every validation ;-)

I think  I've become a legacy :)

* rofl *
0
 
hefterrAuthor Commented:
@agx
True, but that's only 3 lines of code ;-) The string append / counter method has double the code .. for every validation ;-)

Your right (as usual) I surrender LOL


0
 
_agx_Commented:
LOL.  For 2 or 5 validations it probably doesn't make a difference. But .. it's fun discussing it like it's the end of the world ;-)
0
 
cfEngineersCommented:
Well let it be known, that I would not do it that way either, but I was just answering the request as it was requested ;-)
0
 
hefterrAuthor Commented:
@cfEngineers
ROFL - well thanks for giving me  solution you wouldn't use!
0
All Courses

From novice to tech pro — start learning today.