The messageService component is what I was originally thinking about.
Further, I wanted to maintain the messages in a separate XML file that could
be easily edited by whomever wanted to modify the messages (so they wouldn't
have to dive into source code). This XML file is read in and a structure is
created from the various elements. I've attached the XML that I am thinking
about using (I'm really open to suggestions on this one). This creates a
very deep structure... getting the validation message for survey.name being
required looks like:

messages.survey.properties.name.errors.validation.required

I added the extra keys incase I needed to expand the types of messages that
are needed.

After the message stuff is figured out, I'm using metadata in my business
objects for validation. I am adding extra attributes like maxLength,
required and such to <cfproperty> tags. I add a <cfproperty> tag for each
public property that needs validating.

<cfcomponent displayName="Survey"...>
        
        <cfproperty name="name" type="string" required="yes" maxlen="120" />
        <cfproperty name="endDate" type="date" validate="date" />

        <cffunction name="init"...>
                
                <cfset variables.me = structnew() />
                <cfset variables.me.name = "" />
                <cfset variables.me.endDate = now() />
                <cfset variables.errors = arraynew(1) />
        
        ...

Each business object extends a basic component that contains some common
methods. One of the common methods is validate() which looks like this:

<cffunction name="validate" returntype="boolean"...
                
        <cfset var valid = true />
        <cfset var metaData = getMetaData() />
                
        <!--- loop over the properties and get validation information --->
        <cfloop from="1" to="#arraylen(metaData.properties)#" index="i">
                        
        <cfset propName = metaData.properties[i].name />
                        
                <!--- check required properties --->                    
                ...
                <!--- check maxLength --->
                ...
                <!--- check validate --->
                ...
        
For example, if a property is required I want to append an error message to
and array of errors (which each business object has). If the property fails
the required check I would call another common method that appends the
error:

<cfset addError(newError(propName, "validation", getErrorMsg({THIS IS WHAT I
AM MISSING))) />

I would also like to use the messages for client side validation. For
instance, if were to use <cfinput> or roll my own, I would like to be able
to get the same message to use in those situations:

<cfinput type="text" name="surveyName" required="yes"
message="#getErrorMsg(?)#" />

Thanks,
Phillip


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Dawson, Michael
Sent: Thursday, March 25, 2004 4:10 AM
To: [EMAIL PROTECTED]
Subject: RE: [CFCDev] Messages

If you want this to be completely reusable, why not create the
messageService component that all other components can use?

Basically, it would contain an array or struct of messages and related
lookup codes.

You could have a function "getMessageForCode()" that accepts a numeric value
for the lookup code such as "getMessageForCode(319)" which may return the
string "The primary key value already exists..."

I'm curious how you intend on using something like this.  Would you return
the error string in the function or would you throw an error with the error
string as part of the CFTHROW attributes?

Once we have finished discussing this, it would be nice to see a short,
complete example.

M!ke

-----Original Message-----
From: Phillip Cave [mailto:[EMAIL PROTECTED]
Sent: Wednesday, March 24, 2004 3:48 PM
To: [EMAIL PROTECTED]
Subject: [CFCDev] Messages


Does anyone have any recommendations as to where to store message
strings for a particular component. Essentially what I want to do is not
to have to define the same set of error messages more then once. An
example would be error messages that get displayed to a user. I would
like to use the same messages for both client and server side validation
without hard coding it into source files. 

I thought about having one big messageService component that contained
(or read from a file) all the messages for a system. Then I thought that
having some sort of message component for each business object.

Does anyone have any ideas?

Thanks,
Phillip

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to
[EMAIL PROTECTED] with the words 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by Mindtool,
Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]
<messages>
	<strings>
		<components>
			<survey>
				<tooltip>What are surveys all about.</tooltip>
				<tooldescription>Describe a survey here.</tooldescription>
				<properties>
					<name>
						<errors>
							<validation>
								<required>Survey name is required.</required>
								<length>Survey name is too long. Please limit to 120 characters.</length>
								<invalid />
							</validation>
						</errors>
						<tooltip>Enter the name of the survey.</tooltip>
					</name>
					<description>
						<errors>
							<validation>
								<length>Survey description is too long. Please limit to 512 characters.</length>
							</validation>
						</errors>
					</description>
					<enddate>
						<errors>
							<validation>
								<invalid>The survey end date must be a valid date.</invalid>
							</validation>
						</errors>
					</enddate>
					<worksites>
						<errors>
							<validation>
								<required>The survey must be assigned to at least one worksite.</required>
							</validation>
						</errors>
					</worksites>
				</properties>	
			</survey>
			<user>
				<tooltip>Users tooltip</tooltip>
				<tooldescription>Users description.</tooldescription>
				<properties>
					<firstname>
						<errors>
							<validation>
								<required>First name is required.</required>
							</validation>
						</errors>
					</firstname>
				</properties>	
			</user>
		</components>	
	</strings>
</messages>	

Reply via email to