Daniel Roberts wrote:
That makes sense and was a way I considering approaching this but didn't feel right due to the idea that cfcs/classes are supposed to be self documenting.Consider a property called quantity. In human terms, it's pretty clear that quantity is supposed to be a number. And yet, for an object that needs to interact with web forms and handle null values from a DB, an argument type of string is correct and accurately self-documenting. You can use the hint for more information if needed. Again, consider a property called quantity in a object with methods getQuantity() and setQuantity(). In your DB, the quantity field is a numeric and Null is allowed on the field. So a) you need to make sure it's a numeric before persisting it. And b) CF will hand you an empty string if you query the DB and quantity is null. Here's your setter that will accept any possible value from either a web form or your DB <cffunction name="setQuantity" access="public" returntype="void" output="false"> <cfargument name="quantity" required="true" type="string" /> <cfset variables.quantity = arguments.quantity /> </cffunction> Here's a simple example of a validate() method that would live in the same object. <cffunction name="validate" access="public" returntype="boolean" output="false"> <cfset var isValid = true /> <cfif NOT isNumeric(variables.quantity)> <cfset isValid = false /> <cfset variables.validationMessage.quantity = "Please insert a number in the field below"> <cfelse> <cfif variables.quantity LT 1> <cfset isValid = false /> <cfset variables.validationMessage.quantity = "Please insert a quantity greater than 0 in the field below"> </cfif> </cfif> <cfreturn isValid /> </cffunction> <cffunction name="getValidationMessage" access="public" returntype="struct" output="false"> <cfreturn variables.validationMessage /> </cffunction> Your controller would call validate() after populating the (session-scoped) object from a form, and if it returns false, redirect the user back to the form, which would display the appropriate validation message. This is just a quick, simple example. Even if we could work with NULLs in CF, you still couldn't type setQuantity() as numeric and be able to accept values from a web form. You'd need to build 2 objects - a bean for the form and a business object for the rest of the application that would only be populated from a validated bean. Some people go that route, but i don't see the need. As a personal preference, i like simple solutions. :) n.
|
- [CFCDev] How to deal with "empty" objects withou... Daniel Roberts
- Re: [CFCDev] How to deal with "empty" objec... Nando
- Re: [CFCDev] How to deal with "empty" o... Daniel Roberts
- Re: [CFCDev] How to deal with "empty&quo... Nando
- RE: [CFCDev] How to deal with "empty" o... Andrew Powell
- Re: [CFCDev] How to deal with "empty" objec... Ron Phillips
- RE: [CFCDev] How to deal with "empty" objec... Ron Phillips
- RE: [CFCDev] How to deal with "empty" o... Joseph Flanigan
- Re: [CFCDev] How to deal with "empty&quo... Barry Beattie
- RE: [CFCDev] How to deal with "empty... Phillip Senn


