Resource bundle in a model class.

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

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?

Somehow my last email came out in blank. :frowning:

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

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

Thanks!!! we got it working!

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

Thanks!

JC

You can access getResource() from everywhere.

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

Ernst