Question

In my "link" model I have some basic validation, one of which is to check if the link the user is submitting is already in the database.

If the link IS already submitted to the database, I want to let them know that and forward them to the link that was previously submitted (to a URL basically).

How can I do this? My model looks like this so far:

<cfcomponent extends="Model" output="true">

    <cffunction name="init">

        <cfset validatesPresenceOf( property='linkURL') />
        <cfset validatesFormatOf( property='linkURL', type='url', message="Your link isn't a valid URL.") />
        <cfset validatesUniquenessOf( property='linkURL') />

    </cffunction>

</cfcomponent>

Very basic. The validatesUniquenessOf() works great, but I'd like to do a bit more in my validation logic. If I was doing it without the framework...I'd of course do some standard logic, but I'd like to work the way wheels needs me to.

Thanks again CFWHEELS!

Was it helpful?

Solution

This falls out of the general use case for validatesUniquenessOf(), but there are ways around that using addError and errorsOn.

I would do this in the model:

<cfcomponent extends="Model">

    <cffunction name="init">
        <cfset validatesPresenceOf( property='linkURL') />
        <cfset validatesFormatOf( property='linkURL', type='url', message="Your link isn't a valid URL.") />
        <cfset validate("validateUniqueUrl") />
    </cffunction>

    <cffunction name="validateUniqueUrl" access="private">
        <cfscript>
            if (this.exists(where="linkURL='#this.linkURL#'")) {
                this.addError(property="linkURL", name="linkExists", message="The link you entered already exists.");
            }
        </cfscript>
    </cffunction>

</cfcomponent>

The reason I would do that is so that you'll have a named error to check for in the controller (called linkExists).

Then in your controller:

<cfcomponent extends="Controller">

    <cffunction name="create">
        <cfscript>
            link = model("link").new(params.link);

            local.linkExistsErrors = link.errorsOn(property="linkURL", name="linkExists");

            if (link.save()) {
                // Whatever you want to do on success
            }
            else if (ArrayLen(local.linkExistsErrors)) {
                flashInsert(error=local.linkExistsErrors[1].message);
                Location(url=link.linkURL, addToken=false); // Need to use Location or cflocation for hard-coded URLs
            }
            else {
                // Whatever you want to do on other types of validation failures
            }
        </cfscript>
    </cffunction>

</cfcomponent>

API Resources

OTHER TIPS

Could you not supply the submitted url as a link in the message attribute of validatesUniquenessOf()? This way the user will get the error and will be able to follow the link in the message. Otherwise, I think you will need to use cflocation to send the user to the linUrl value if the validatesUniquenessOf() functions returns false.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top