Form validation using CFCs

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Form validation using CFCs

    I have a pretty nice method for coding forms using server side validation. One
    <label /><input /> pair might look like so:

    <label for="foo">Foo!</label>
    <input type="text" name="foo" id="foo" value="<CFif
    isDefined('form.foo')>#form.foo#<CFelse>#query.foo #</CFif>" />

    Now, this would be for something like an 'edit' form, where initially we'll
    want to populate the field with the query value, but use the posted value if
    they tried to submit the form but something was wrong. Once a form is posted,
    I'll have the values run through an include called something like
    validate-forms.cfc. This will contain a switch that checks the value of
    #form.toValidate# which would have been supplied as a hidden field of the form.
    Within' each case is a series of checks for empty values, invalid (against
    regex) values and so forth. When errors are found, I save them in a struct
    called something like #errors#. Then, if there are any problems with the form,
    I can notify for each field like so:

    <label for="foo">Foo!</label>
    <input
    type="text"
    name="foo"
    id="foo"
    value="<CFif isDefined('form.foo')>#form.foo#<CFelse>#query.foo #</CFif>"
    <CFif structKeyExists(errors,'foo')> class="empty" title="#errors.foo#"</CFif>
    />

    Now, recently I've been moving over to stronger use of CFCs and other levels
    of seperation and abstraction with CF. I'd like to use a CFC to process the
    form and validate it, for instance, but I don't understand how I'd accomodate
    the rest of what I have going on here. Once values are passed to a CFC, if
    there are errors, how can I return the user to the view page while retaining
    knowledge of the posted values as well as the errors created?

    mate of the state Guest

  2. Similar Questions and Discussions

    1. FORM scope and CFCs?
      I have a application where users take a 90 question multiple choice test, after each of the 6 pages I want to save their answers to an array for...
    2. CFFORM Validation trumping Custom Form Validation
      Is there any way for custom form validation to work in concert with the cfform validation? I have a custom script that compares the values of two...
    3. Form Validation
      Hi all, I am using form validation to authenticate users. In the web config file I secure a page that has only the following statement:...
    4. copy and paste form RTF document into field in asp form cause it to bypass field length and javascript validation - how to overcome?
      I have a web form with several fields. If I copy & paste from a RTF document into a field, the javascript validation and field length are bypassed...
    5. get rid of form validation
      I added form validation in MX and then deleted some form fields, so needed to change the validation behavior. I deleted the On blur from the...
  3. #2

    Default Re: Form validation using CFCs

    The same way your view page is seeing the 'errors' struct now. You can create the struct by CFC function calls and/or return a struct from a function call.
    travelinrob Guest

  4. #3

    Default Re: Form validation using CFCs

    One of us is not understanding something... I'll just explain more what I see
    as the problem I don't know how to get around, and maybe you or somebody else
    can clarify things further:

    User fills out a form. The form posts to a CFC. The CFC validates the form,
    creating a struct called #errors# if any are found. Now, once that's done, the
    CFC will want to direct the user to the appropriate view page using
    <CFlocation>. Once they are at the new page, how can I still have knowledge of
    those posted #form# variables as well as the #errors# struct? The only way I
    understand how to pass along data using <CFlocation> is via the query string,
    or perhaps a persistent scope like session. Neither of those seems appropriate
    here, though.

    mate of the state Guest

  5. #4

    Default Re: Form validation using CFCs

    I wouldn't post to a CFC. I would post to a process page that creates an
    instance of the CFC and calls it's functions. In the process page you can then
    include, upon successful checking, the page with your forms. The forms page
    will then see the structure(s).

    travelinrob Guest

  6. #5

    Default Re: Form validation using CFCs

    Okay, but then it seems like we're not far from the self-posting forms approach
    I'm exploring getting away from. With your example, why would I want to do that
    rather than the same thing, but all within' the same template?

    mate of the state Guest

  7. #6

    Default Re: Form validation using CFCs

    CFCs can be used for generic code that can be used across multiple templates.
    For instance, you wanted to check for nulls or other reg expressions. That is
    something you will do with many templates. Each of those templates can make an
    instance of your validate CFC and call the functions that are appropriate to
    each. Then, you don't have to write your validate code multiple times. Or, you
    might make a CFC to contain a group of functions for a particular project. All
    the functions (components) of that application can reside in one CFC, or
    multiple, if that makes sense. It doesn't make sense to put everything in a
    CFC. But a page of commented function calls is much cleaner than a page of code
    everywhere.

    travelinrob Guest

  8. #7

    Default Re: Form validation using CFCs

    I understand and am with you on all that, though I think you have given me
    insight into a better way to structure my validation CFC(s), so thanks for
    that. :)

    Could you elaborate on self-posting vs. not, though? As I said, I think I can
    understand seperating into a processing template for organization's sake, but I
    guess I got the impression there was more to it. I've read that self-posting is
    bad form (ha) and should be deprecated. But with the functionality you discuss,
    I wonder why I shouldn't just post back to the same template. Any thoughts on
    this?

    mate of the state Guest

  9. #8

    Default Re: Form validation using CFCs

    I have not read anything on the self-posting debate. I would need to do some research and interpret both sides before I could answer that for you. I still self-post.
    travelinrob Guest

  10. #9

    Default Re: Form validation using CFCs

    To be clear, in self-posting I mean the I have a main index that includes
    templates based on both url and form parameters. But, mostly all through one
    page except for pop ups and unrelated by-products.

    travelinrob Guest

  11. #10

    Default Re: Form validation using CFCs

    That's fine. I think most of the reasons for getting away from it have to do
    with going towards smarter levels of abstraction and seperation, but a lof of
    the solutions there have to do more with processing and abstract ways of
    thinking about your code. Physical organization is a pretty open ended,
    personal topic, I suppose.

    To be clear, this is what I'm thinking when I think of a self-posting form. I
    will usually have an action template for an object, let's say, a product.
    Within that, there'd be an option switch, of which one case would be "update."
    The update case handles both new inserts and updates to existing records.
    Something like...

    <CFquery name="foo" datasource="#request.dsn#">SELECT * FROM foos WHERE id =
    <CFif isDefined('url.id')>#url.id#<CFelse>-1</CFif>

    <CFif isDefined('form.toValidate') and form.toValidate is 'fooform'>

    <!--- process posted data --->

    <CFelse>

    <!--- form not posted, show form --->

    </CFif>

    mate of the state Guest

  12. #11

    Default Re: Form validation using CFCs

    I have similar code, except I have includes below your comments. I keep all of my sub-templates in a tpl directory.
    travelinrob Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139