Link to home
Start Free TrialLog in
Avatar of hefterr
hefterrFlag for United States of America

asked on

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

SOLUTION
Avatar of cfEngineers
cfEngineers

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of hefterr

ASKER

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!
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 ;-)
Avatar of hefterr

ASKER

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
Avatar of hefterr

ASKER

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

I think  I've become a legacy :)
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 *
Avatar of hefterr

ASKER

@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


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 ;-)
Avatar of cfEngineers
cfEngineers

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 ;-)
Avatar of hefterr

ASKER

@cfEngineers
ROFL - well thanks for giving me  solution you wouldn't use!