Resource bundle in a model class.

15 views
Skip to first unread message

overcrow

unread,
Jun 23, 2009, 10:46:49 PM6/23/09
to ColdBox Framework
Hi,

This may sound a stupid question, but we are trying to figure out a
way to instantiate the resource bundle inside a class (component) in
our model.

We are creating a class to validate the forms and there is a method in
this class that returns the error messages that will be processed in
the handler. It's working fine, but now we need to have this class
calling the resource bundle in order to properly get the message in
the resource bundle, before sending to the handler.

He have the following method in the validation class:

<cffunction name="init" returntype="void" output="false"
displayname="Init" hint="Form Validation Constructor" >
<cfset This.valid = true>
<cfset This.errorMessages = ArrayNew(1)>
<!--- ISSUE IS HERE --->
<cfset This.resources = CreateObject
("component","coldbox.system.plugins.resourceBundle")>
</cffunction>

<cffunction name="isRequired" access="public" output="false"
returntype="void" displayname="Required Field" hint="Check if the
value of required field is not blank">
<cfargument name="name" type="string" required="true"
displayname="Name" hint="Name of the field to be validated">
<cfargument name="value" type="string" required="true"
displayname="Value" hint="Value of the field to be validated">

<cfif trim(Arguments.value) eq "">
<cfset This.valid = false>
<cfset ArrayAppend(This.errorMessages,Arguments.name &
This.resources.getResource("manageLanguages.create")) /
>
</cfif>

</cffunction>

How can we achieve such thing using the framework / best practices ?
This class does extends any other class from the framework.

Thank you very much!

Julio Lima

Luis Majano

unread,
Jun 24, 2009, 1:04:01 AM6/24/09
to col...@googlegroups.com
Julio,

By creating the rb class in your model you will loose the translations, since they are kept by the coldbox settings.  The only way to achieve this is to inject the plugin into your model via dependency injection.

Are you using Model Integration, Coldspring or lightwire?
--
Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldboxframework.com
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com
Message has been deleted

overcrow

unread,
Jun 24, 2009, 11:22:14 AM6/24/09
to ColdBox Framework
Somehow my last email came out in blank. :(

This is our first time using LightWire and we are king lost :/.

We tried the example provided in the documentation (security/user
manager) also following the LightWire Guide.

We are created our LightWire.cfc and updated the config.xml.cfc.

Tried to use resource bundle in the class model, did not work :(.

Note: We turn of the Autowire, we should not use the autowire if we
have LightWire, is the correct?

Our code:

This is FormValidation test sample class:

<cfcomponent displayname="Form Validation" hint="Form Validation
Component" output="false">

<cfproperty name="valid" type="boolean" default="true"
displayname="Valid" hint="Status of the validation - default is true">
<cfproperty name="errorMessages" type="array" required="true"
displayname="Error Messages" hint="Array of error messages to be
processed by messagebox pluggin">
<cfproperty name="resourceBundle"
type="coldbox:plugin:ResourceBundle" scope="this">

<cffunction name="init" returntype="void" output="false"
displayname="Init" hint="Form Validation Constructor" >
<cfset This.valid = true>
<cfset This.errorMessages = ArrayNew(1)>
</cffunction>

<cffunction name="isRequired" access="public" output="false"
returntype="void" displayname="Required Field" hint="Check if the
value of required field is not blank">
<cfargument name="name" type="string" required="true"
displayname="Name" hint="Name of the field to be validated">
<cfargument name="value" type="string" required="true"
displayname="Value" hint="Value of the field to be validated">

<cfif trim(Arguments.value) eq "">
<cfset This.valid = false>
<cfset ArrayAppend(This.errorMessages,Arguments.name &
GET_RESOURCE_BUNDLE_TEXT) />
</cfif>

</cffunction>


<cffunction name="getErrorMessages" returntype="Array" output="false"
displayname="Get Error Messages" hint="Return error messages">
<cfreturn This.errorMessages />
</cffunction>

</cfcomponent>

In our handler call we have:

<!--- Form Validation --->
<cfobject component="org.iadb.knlsystem.model.util.FormValidation"
name="validator">
<cfset validator.init(This)>

<!--- 1. Validate if Description is blank --->
<cfset validator.isRequired("Description",rc.description)>

<!--- 2. Display error messages if any --->
<cfif ArrayLen(validator.errorMessages)>
<cfset getPlugin("messagebox").setMessage("Error","Error(s): <br /
>",validator.errorMessages) />
</cfif>

So we want the model to access the resource and return error messages
(GET_RESOURCE_BUNDLE_TEXT).

Any easy steps on how to proceed with LightWire?


Thanks....

JC






Ernst van der Linden

unread,
Jun 24, 2009, 11:29:37 AM6/24/09
to col...@googlegroups.com
RB Example:

ArrayAppend(rc.validationMessages,getResource('MyResourceBundleKey'));

LightWire.cfc ColdBox injection example:

// User Service
addSingleton("model.services.UserService","userService");
addConstructorProperty("userService","coldbox",getController() );


Hope this helps.

Ernst

overcrow

unread,
Jun 24, 2009, 2:08:39 PM6/24/09
to ColdBox Framework
Thanks!!! we got it working!

One more thing :) Is there a easy way to load the bundle in the
layout?

Thanks!

JC

On Jun 24, 11:29 am, Ernst van der Linden <evdlin...@gmail.com> wrote:
> RB Example:
>
> ArrayAppend(rc.validationMessages,getResource('MyResourceBundleKey'));
>
> LightWire.cfc ColdBox injection example:
>
>                 // User Service
>                 addSingleton("model.services.UserService","userService");
>                 addConstructorProperty("userService","coldbox",getController() );
>
> Hope this helps.
>
> Ernst
>

Ernst van der Linden

unread,
Jun 24, 2009, 2:48:44 PM6/24/09
to col...@googlegroups.com
You can access getResource() from everywhere.

Just to let you know, bundle is loaded on framework init.

Ernst
Reply all
Reply to author
Forward
0 new messages