ColdFusion - ValidationExceptions

I've been in java training for a while now, and within java the best practice is to throw an Exception anytime you want to return a message from one method to the calling method. So for example, a method doValidate would return true/false, but we would actually want the messages of what failed validation if it returned false. Well instead of returning false we would throw an exception.

Well in base coldfusion, the only thing we can do is throw one message at a time, so it doesn't make sense to code it that way. Well if you are like me, I really like the throw concept, an it doesn't make sense to add a bunch of code just to return false w/ a structure of the messages!!!

<cfthrow errorcode="" message="" detail="" extendedInfo="">


That being said, and since Coldfusion is built on top of java, all you need to do is write a java program that extends RunTimeException. I have done just that were the Java Class has the capability of storing multiple messages. And then you can throw that object as the error.

<cfthrow obj="">


Here is my Java Code
import java.util.ArrayList;

public class ValidationException extends RuntimeException {
     private ArrayList validationError = new ArrayList(); // [ExceptionNumber][errorCode | message | detail | extendedInfo]

    public ValidationException() { super(); }

    public ValidationException(String message) { super(message); }

    public boolean hasValidationError() {
        if (validationError.size() == 0) {
            return false;
        } else {
            return true;
        }
    }

    public void addValidationError(String errorCode, String message, String detail, String extendedInfo) {
        ValidationError curError = new ValidationError(errorCode, message, detail, extendedInfo);        
        validationError.add(curError);
    }
    
    public void addValidationError(String message) {
        addValidationError("", message, "", "");
    }
    
    public ValidationError getValidationError(int x) {
        return (ValidationError)validationError.get(x);
    }
    
    public int length() {
        return validationError.size();
    }
}


Here is my Example Coldfusion Code

METHOD WITHIN CFC

<cffunction name="doValidate" output="true" access="public" hint="validation of campaign Setup">
        <cfset var eValidate = "">
        <cfset eValidate = CreateObject("java", "edu.psu.uao.exceptions.ValidationException").init("Validation Error")>
        
        <cfif trim(variables.instance.CampaignName) EQ "">
            <cfset eValidate.addValidationError("cfc.service.crm.campaign.doValidate.1","Campaign Name is Required","","")>
        </cfif>        
        <cfif trim(variables.instance.CampaignActive) EQ "">
            <cfset eValidate.addValidationError("cfc.service.crm.campaign.doValidate.2","Campaign Active is Required","","")>
        </cfif>
        <cfif trim(variables.instance.campaignTypeID) EQ "">
            <cfset eValidate.addValidationError("cfc.service.crm.campaign.doValidate.3","Campaign Type is Required","","")>            
        </cfif>
        <cfif trim(variables.instance.campaignPopulationDelay) EQ "">
            <cfset eValidate.addValidationError("cfc.service.crm.campaign.doValidate.4","Campaign Population Delay is Required","","")>
        </cfif>
        <cfif trim(variables.instance.campaignPopulationRefresh) EQ "">
            <cfset eValidate.addValidationError("cfc.service.crm.campaign.doValidate.5","Campaign Population Refresh is Required","","")>
        </cfif>
        <cfif trim(variables.instance.CampaignPoolID) EQ "">
            <cfset eValidate.addValidationError("cfc.service.crm.campaign.doValidate.6","Campaign Pool is Required","","")>
        </cfif>
        <cfif trim(variables.instance.CreatedBy) EQ "">
            <cfset eValidate.addValidationError("cfc.service.crm.campaign.doValidate.7","Campaign Created By is Required","","")>
        </cfif>
        
        <cfif eValidate.hasValidationError()>
            <cfthrow object="#eValidate#">
        </cfif>

    </cffunction>



CALLING CFM PAGE



<cftry>

    <cfset oCamp.doValidate()>
    

    <cfcatch type="edu.psu.uao.exceptions.ValidationException">        
        <cfset ve = cfcatch>
        <cfoutput>
        <cfloop index="x" from="0" to="#Val(ve.length()-1)#">
            <cfset cve = ve.getValidationError(javaCast('int',x))>
            errorCode: #cve.getErrorCode()#
            message: #cve.getMessage()#
            details: #cve.getDetail()#
            extendedInfo: #cve.getExtendedInfo()#
        </cfloop>
        </cfoutput>
    </cfcatch>
    <cfcatch type="Any">
        all done
<cfdump var="#cfcatch#">
    </cfcatch>
</cftry>

Comments

Popular posts from this blog

ColdFusion in an Enterprise Environment - Part 1 - Understanding how to use SubVersion (SVN)

coldfusion builder 2 extension not displaying browse button on type=projectdir

Being Thread Safe in Coldbox/Coldfusion