8 Replies Latest reply on Apr 22, 2010 2:58 PM by Adam Cameron.

    Accessing cfc via form

    fs22 Level 1

      I am dipping my toe into my first cfc...trying to get more oop oriented.


      I have a cfc that I am developing for a login to will check if a user name exists in the db

      ...so that is one function.


      I have a second function that accesses a web service from our Oracle environment that returns a boolean if they customer number is accurate and is on support.All of our customers know their number.


      So I created a registration form with a user name field and a customer number field. In my action attribute, I reference my cfc as follows...




      This seems to work fine for one function. I have two functions as I mentioned, so I thought I could "daisy-chain" the methods...




      This did not work. Seems that it looks for a method that is chkUser,Oraclechk, which of course, does not exist.


      So here are my questions...


      1. Is there a way to submit multiple methods through the action?
      2. If not, then I think the best thing would be to combine my functions into one. The only problem is that one function (chkUser) returns a string, which tells the end user that the user name exists [my plan was to perform a cfif within my processing to display that to the end user]. The other function simply returns a status of active or inactive.







        • 1. Re: Accessing cfc via form
          Dan Bracuk Level 5

          A better way is to not submit forms to cfc pages.  Instead, submit them to cfm pages which call the cfc pages.

          1 person found this helpful
          • 2. Re: Accessing cfc via form
            fs22 Level 1

            ok...I was simply following an article written by Ben Forta. I am fine doing it another way...Do you have an example somewhere?

            • 3. Re: Accessing cfc via form
              Dan Bracuk Level 5

              My approach would be:


              Use cfobject or createobject to get access to the methods in the cfc.

              Call the two functions separately.  You can use cfset or the equivalent cfscript code for this.

              Process the returned information according to your requirements.

              • 4. Re: Accessing cfc via form
                fs22 Level 1

                Thanks for pointing me in the right direction. I believe I have it figured out. I would appreciate any feedback on my code though

                , since this is my first cfc. I will see if I can attach.


                One thing I am still fuzzy on is the need for init().


                Cannot seem to attach code...










                • 5. Re: Accessing cfc via form
                  ilssac Level 5

                  the init() is not needed.


                  But the ColdFusion Component does not have the automatic constructor method that many other Object Orientated Systems do.  It is the psudo constructor area.  Which is code outside of any <cffunction...> block.  Code placed here, ususally at the top of the <cfcomponent...> block, will be ran for every invokation of the component.  But one can not provide any parameters to this function.


                  Thus, with a need for a constructor method, the ColdFusion community as set on a standard of the init() function.  That can be called when one invokes the component, to initilze the object with desired parameters.

                  1 person found this helpful
                  • 6. Re: Accessing cfc via form
                    fs22 Level 1

                    OK...I get it (me thinks)....So it is a good practice to create an init

                    function in every cfc even though it is not needed as I could place code inside the init that would fire every time the cfc is instantiated.


                    Thanks you have been very helpful. I am attempting to get more oop oriented and separate my cf code more logically (MVC and such)

                    • 7. Re: Accessing cfc via form
                      ilssac Level 5

                      Well, I wouldn't create one if I did not need it in a specific Component.


                      But if your component needs one, the CF community standard is to name it init() so the chances are better that your code would be understood by others if you used init() rather then start() or constructor() or some other random function name.

                      • 8. Re: Accessing cfc via form
                        Adam Cameron. Level 5

                        Note that when using CF9's Hibernate integration, init() methods are treated as "proper" constructors, in that they are called automatically when an entity is created.


                        So using init() is moving away from being a de facto standard approach to this, and moving more towards an actual "official" implementation.  If that makes sense.


                        If I have a CFC that will have stateful instances, I'll always implement an init() method, and always call it when I use the CFC.  Even if all the init() does is "return this".  I'm not sure there's really a good reason for doing this, though, I have to admit.


                        For CFCs that are simply a collection of like-minded functions, I don't bother.