rule type="custom"

13 views
Skip to first unread message

tarekac

unread,
Sep 25, 2009, 6:49:42 AM9/25/09
to ValidateThis
Hi,

I'm working on the server-side validation with no success, I am trying
2 different types of validation to see wheich one works best for me,
one that uses the "custom" type and one that uses a condition I have
set up.

They are not setup in my xml at the same time.

So for one type of validation, I am using the "custom" type:

<property name="uniqueName">
<rule type="custom" failureMessage="That name is already taken.
Please try a different name.">
<param methodname="studyExists" />
<param remoteURL="checkStudyExists.cfm" />
</rule>
</property>

ok, so:

1. how do values get passed into studyExists() ?
2. i'm confused as to what remoteURL would be doing here, this would
be more server-side validation surely???
3. i presume the remoteURL can also be a Fusebox circuit?

Ok for my alternate type of ss-validation, I've tried a condition
setup:

<condition name="uniqueAcronym" serverTest="NOT studyExists
('#attributes.someStudyCode#')" />

I know VT finds studyExists() method ok (I get method not found if I
change the name), but what do I need to check when the page is
submitted?

btw: studyExists() simply returns true/false based on a query, i've
tested it so i know it returns correct result.

I have also tried creating a new Transfer object, doing something like
tr.setStudyCode('SOME VALUE I KNOW ALREADY EXISTS') and running
VT.validate(), but I only see an empty struct returned.

Thanks in advance, Tarek.

Bob Silverberg

unread,
Sep 25, 2009, 10:09:44 AM9/25/09
to valida...@googlegroups.com
On Fri, Sep 25, 2009 at 6:49 AM, tarekac <ta...@auniquedomainname.com> wrote:
>
> Hi,
>
> I'm working on the server-side validation with no success, I am trying
> 2 different types of validation to see wheich one works best for me,
> one that uses the "custom" type and one that uses a condition I have
> set up.
>
> They are not setup in my xml at the same time.
>
> So for one type of validation, I am using the "custom" type:
>
> <property name="uniqueName">
>        <rule type="custom" failureMessage="That name is already taken.
> Please try a different name.">
>                <param methodname="studyExists" />
>                <param remoteURL="checkStudyExists.cfm" />
>        </rule>
> </property>
>
> ok, so:
>
> 1. how do values get passed into studyExists() ?

They don't. You cannot pass any values into a server-side validation.
The values must be resident in the object already. In this example
you would populate your object, calling setUniqueName() and passing in
what the user entered. Then you code your studyExists() method to
look at that property and compare it to something. For example:

<cffunction name="studyExists" access="public" output="false"
returntype="any" hint="Checks for a duplicate Study.">

<cfset var ReturnStruct = {IsSuccess = false, FailureMessage = "That
Study has already been used. Try to be more original!"} />
<cfif getStudyService().isUniqueName(getUniqueName())>
<cfset ReturnStruct = {IsSuccess = true} />
</cfif>
<cfreturn ReturnStruct />
</cffunction>

That method does the following:

1. Defaults the return structure to "false", and sets the failure
message message.
2. Passes the value stored in the UniqueName property to a method in a
StudyService object, which would check the uniqueness of that name.
If it is unique it resets the return structure to "true".
3. Returns the return structure.

Note that the built-in server validator object will check the value
returned from this method - you don't have to do that yourself.

Also note that you could make the whole thing "less encapsulated" by
simply including the logic to check for a unique name inside this
studyExists() method if you wish (rather than calling a composed
StudyService). The bottom line is that some logic must be run which
compares the value of a property to something else, and then a
structure must be returned.

> 2. i'm confused as to what remoteURL  would be doing here, this would
> be more server-side validation surely???

remoteURL is there to support client-side validations via the jQuery
plugin's remote method. It is meant to allow for AJAX validation,
where the logic is run on the server and then reported back to the
client. Let me look closer at how it works (I wrote it awhile ago)
and I'll respond back with more details.

> 3. i presume the remoteURL can also be a Fusebox circuit?
>

See above.

> Ok for my alternate type of ss-validation, I've tried a condition
> setup:
>
> <condition name="uniqueAcronym" serverTest="NOT studyExists
> ('#attributes.someStudyCode#')" />
>
> I know VT finds studyExists() method ok (I get method not found if I
> change the name), but what do I need to check when the page is
> submitted?
>

Just like the custom rule example, you cannot pass anything into a
condition. You are trying to pass attributes.someStudyCode in your
example. You would need to do something to get someStudyCode into
your object. for example:

setUniqueName(attributes.someStudyCode);

and then just reference that property. So your condition would just look like:

<condition name="uniqueAcronym" serverTest="NOT studyExists()" />

and you would code studyExists() as described above.

Note that this is probably not the behaviour you want. A condition
says "only use this validation rule if this condition is true". So
you'd only want to define a condition like that if you wanted a rule
(or a set of rules) that are only enforced if a study already exists.
If that is what you are trying to do, then yes, a condition is the
right way to go, but if you're simply trying to add a validation that
says "the name of this study must be unique", then you'd use a custom
rule for that.

> btw: studyExists() simply returns true/false based on a query, i've
> tested it so i know it returns correct result.
>

Not from the example above, the custom rule actually expects a
structure to be returned with a key called "IsSuccess". That is how
the API is defined currently. I did that to allow for failure
messages to be generated from inside the custom method. But I suppose
a simpler API, if one doesn't need custom error messages, would be to
simply allow for the custom method to return true or false. I'll look
into adding that as a feature in the next release. For now, if you
can test using the API as described above (in the sample code for
studyExists()) that would be great.

> I have also tried creating a new Transfer object, doing something like
> tr.setStudyCode('SOME VALUE I KNOW ALREADY EXISTS') and running
> VT.validate(), but I only see an empty struct returned.
>

Right. That may be because of what studyExists() returns. Again, try
coding is as in the example above, returning a struct with a key
called "IsSuccess", and see if it then works as expected.

I know this stuff is a bit complicated when first starting, and I
haven't properly documented either of these features yet. Please let
me know if what I've written here makes sense, and if you have any
further questions. If what I've written doesn't help you to make it
work, please post back with the following:

- your rules xml file
- the code of the studyExists() method

Thanks,
Bob

--
Bob Silverberg
www.silverwareconsulting.com

Reply all
Reply to author
Forward
0 new messages