<<

OOP

Hands On 17

By Simon Free (Bio)
gist

In this Hands On we are going to create an encapsulated cfc that holds error data.

Functions Used: ArrayAppend, ArrayLen

  1. Create a new file called errorBean in the /www/admin/cfc/ folder.
  2. Open up the /www/admin/cfc/errorBean.cfc file in your code editor.
  3. This cfc will be written entirely in cfscript so create a cfscript based component declaration. Your code should look similar to this:
    component{
    }
    
  4. The first function we are going to create is the init function. This function will instantiate the internal variables and return itself. Create a function called init that is public and returns a variable type of errorBean.
  5. Inside the function create a variable declaration called variables.errors and instantiate an empty array.
  6. Below this return the this scope. Your function should look similar to this:
    public errorBean function init(){
    	variables.errors = [];
    	return this;
    }
    
  7. Next create a function called addError that is public and returns void. Create 2 required string arguments called message and field.
  8. Inside the function append a struct to the variables.error variable. The structure should consist of 2 keys, message and field. Set each key to their related argument value. This function will be used to add errors to the object. Your function should look similar to this:
    public void function addError(required string message, required string field){
    	arrayAppend(variables.errors,{message=arguments.message,field=arguments.field});
    }
    
  9. Next we will create a function called getErrors which will return all errors stored in the object. The function will be public, return an array, and accept no arguments.
  10. Inside the function return the variables.errors variable. Your function should look similar to this:
    public array function getErrors(){
    	return variables.errors;
    }
    
  11. We now need to create a function that will tell us if there are any errors stored in the object. Create a public function called hasErrors. The function will return a boolean value and will accept no arguments.
  12. Inside the function create an if statement that checks the array length of the variables.error array. If there is a length, return true. If there is not a length return false. Your function should look similar to this:
    public boolean function hasErrors(){
    	if(arrayLen(variables.errors)){
    		return true;
    	}
    	else{
    		return false;
    	}
    }
    
  13. Finally we will create a function that removes all errors from the object. Create a function called clearErrors that is public, does not return anything and accepts no arguments.
  14. Inside the function assign an empty array to the variables.errors variable.
  15. You have now created an encapsulated object. We will test that this object works in one of the other Hands On’s but for now confirm that your cfc looks similar to the following:
    component{
    	public errorBean function init(){
    		variables.errors = [];
    		return this;
    	}
    		
    	public void function addError(required string message, required string field){
    		arrayAppend(variables.errors,{message=arguments.message,field=arguments.field});
    	}
    		
    		
    	public array function getErrors(){
    		return variables.errors;
    	}
    		
    	public boolean function hasErrors(){
    		if(arrayLen(variables.errors)){
    			return true;
    		}
    		else{
    			return false;
    		}
    	}
    	
    	public void function clearErrors(){
    		variables.errors = [];
    	}
    }