Phil,

 

Thanks. I didn’t mean to be so specific about my validation, I was just looking for an approach to validation in general. For instance, if I want to ensure that the password is at least 6 characters. Is this a check that should be put in the Record object somewhere, or in the controller? My problem with the controller, is that anywhere I set the password, I have to do my check, but if it was in a record it could be done in 1 place.

 

How are you guys doing this validation?

 

Cedric

 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Phil Cruz
Sent: Saturday, February 25, 2006 9:39 AM
To: [email protected]
Subject: Re: [Reactor For CF] Validating records

 

You could do a "pre-check" in the controller but that wouldn't guarantee uniqueness because another user with that email could be inserted after that check and before you try to insert.  The only way to guarantee is to put a unique constraint in the database and catch the exception.  You can look at the Mach-II sample application to see one way to implement that.  Look at the UserListener.cfc::

    <cffunction name="createUser" access="public" returntype="void" output="false" displayname="Create User" hint="I cause a user to be created from the current event object.">
        <cfargument name="event" type="MachII.framework.Event" required="yes" displayname="Event" hint="I am the current event" />
        <cfset var user = arguments.event.getArg("user") />       
        <cfset var userData = user.getUserTO() />
        <cfset var errors = structNew() />
        <cftry>           
            <cfset variables.userService.createUser(argumentcollection=userData, dsn=variables.dsn, dbtype=variables.dbtype) />
            <cfset announceEvent("createUser.success", arguments.event.getArgs()) />
        <cfcatch type="Database">
            <cfif findNoCase("duplicate",cfcatch.detail) >
                <cfscript>
                    errors.usernameExists = "That username has been taken.";
                    user.setErrors(errors);
                    announceEvent("createUser.usernameExists", arguments.event.getArgs());
                </cfscript>               
            <cfelse>
                <cfrethrow>
            </cfif>
        </cfcatch>
        </cftry>
    </cffunction>

hth,

Phil

On 2/24/06, Cedric Villat <[EMAIL PROTECTED]> wrote:

Just a quick question about validating records. I have a UserRecord that has a Username, password, and email. Once of the requirements is that the Username must be unique. Should I check for uniqueness in a controller before I call the save() method, or should I use a Validate() method that checks for the uniqueness? If I used a Validate() method, where does it go? Is it ok to reference a UserGateway to call an Exists() function?

 

Just wondering where to do this sort of validation.

 

Cedric

-- Reactor for ColdFusion Mailing List -- [email protected] -- Archives at http://www.mail-archive.com/reactor%40doughughes.net/


-- Reactor for ColdFusion Mailing List -- [email protected] -- Archives at http://www.mail-archive.com/reactor%40doughughes.net/

-- Reactor for ColdFusion Mailing List -- [email protected] -- Archives at http://www.mail-archive.com/reactor%40doughughes.net/

Reply via email to