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/

Reply via email to