Validating collection of objects.

26 views
Skip to first unread message

Rawlins

unread,
Feb 2, 2012, 12:41:54 PM2/2/12
to valida...@googlegroups.com
Hello Guys,

I have a collection of objects which I want to validate. This collection is contained within a container object.

Container.cfc
    Object.cfc
    Object.cfc
    Object.cfc
    Object.cfc
    Object.cfc

Is there any way to define a rule on the Container object that tells VT to validate all of the children within it? Returning the set of errors from the various children?

The children each have their own rule definition - I'm just looking for a quick way to validate them as a collective.

Or is this something I need to write a custom validation rule or method for?

Thanks.

Robert

Bob Silverberg

unread,
Feb 2, 2012, 12:58:01 PM2/2/12
to valida...@googlegroups.com
I think you should be able to accomplish what you want with this:

http://www.validatethis.org/docs/wiki/Validation_Types_Supported_By_ValidateThis.cfm#IsValidObject

Bob

> --
> You received this message because you are subscribed to the Google Groups
> "ValidateThis" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/validatethis/-/n1QFSFbJsNAJ.
> To post to this group, send email to valida...@googlegroups.com.
> To unsubscribe from this group, send email to
> validatethis...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/validatethis?hl=en.

--
Bob Silverberg
www.silverwareconsulting.com

Robert Rawlins

unread,
Feb 2, 2012, 1:28:22 PM2/2/12
to valida...@googlegroups.com
Hi Bob,

I see how that could be applied to validate a child - but how could I define a rule on the Container, so that if I pass the container to VT it validates all the children - giving me their errors?

What I'm trying to do might not be possible, I may have to just write my own validate() method on the Container which loops etc.

Thanks,

Robert

Bob Silverberg

unread,
Feb 2, 2012, 2:12:50 PM2/2/12
to valida...@googlegroups.com
If the container has a property that contains an array of objects, can
you not then use the isValidObject rule on that property?

--
Bob Silverberg
www.silverwareconsulting.com

Rawlins

unread,
Feb 3, 2012, 5:46:55 AM2/3/12
to valida...@googlegroups.com
Hi Bob,

I can do this - however I get some slightly unexpected behaviour.

It appears to only return failures for the last object in the array, rather than every object.

So if I have an array of 3 invalid objects - only failures for the last invalid object are added to the result.

Thanks.

Robert

Bob Silverberg

unread,
Feb 3, 2012, 7:24:10 AM2/3/12
to valida...@googlegroups.com
That's not the way it's supposed to work. Would it be possible for you to send me some code for an isolated test?

Thanks,
Bob

Sent from my mobile
--
You received this message because you are subscribed to the Google Groups "ValidateThis" group.
To view this discussion on the web visit https://groups.google.com/d/msg/validatethis/-/0abzj2kiAh0J.

Robert Rawlins

unread,
Feb 3, 2012, 8:05:33 AM2/3/12
to valida...@googlegroups.com
Hi Bob,

I've been looking at the source for the validation rule this morning.

It seems that it loops over the array - however, it invokes failWithResult() inside the loop when it finds an object which doesn't validate - would this break the loop and end processing? My guess is yes -  in which case we'd need to compile a result for all the collection members within the test before invoking that method to return the collective result set, correct?

I've attached a ZIP containing some test code which should run in isolation - you will need to point to a local VT install in the test.cfm though.

In an ideal world - what I need back from the isValidObject when invoked on an array is not only what specific failures were encountered, but also to which instance within the collection.

This would make placement of the error messages around the appropriate fields possible.

Thanks.

Robert

vtisobjecttest.zip

Bob Silverberg

unread,
Feb 3, 2012, 9:08:44 AM2/3/12
to valida...@googlegroups.com
On Fri, Feb 3, 2012 at 8:05 AM, Robert Rawlins <rob...@weboffins.com> wrote:
> Hi Bob,
>
> I've been looking at the source for the validation rule this morning.
>
> It seems that it loops over the array - however, it invokes failWithResult()
> inside the loop when it finds an object which doesn't validate - would this
> break the loop and end processing? My guess is yes -  in which case we'd
> need to compile a result for all the collection members within the test
> before invoking that method to return the collective result set, correct?
>
> I've attached a ZIP containing some test code which should run in isolation
> - you will need to point to a local VT install in the test.cfm though.
>

Thanks Rob.

> In an ideal world - what I need back from the isValidObject when invoked on
> an array is not only what specific failures were encountered, but also to
> which instance within the collection.
>

That is how it is supposed to work, and I'm pretty sure it's been
tested for that, so I'll have to take another look. I'll try to get
to it today, but it might not be until this weekend.

Bob

> This would make placement of the error messages around the appropriate
> fields possible.
>
> Thanks.
>
> Robert
>
>

> --
> You received this message because you are subscribed to the Google Groups
> "ValidateThis" group.

--
Bob Silverberg
www.silverwareconsulting.com

Robert Rawlins

unread,
Feb 3, 2012, 9:18:00 AM2/3/12
to valida...@googlegroups.com
That's great - thank you so much Bob.

If you need any further information or help feel free to ask.

Robert

Bob Silverberg

unread,
Feb 3, 2012, 1:00:49 PM2/3/12
to valida...@googlegroups.com
I just ran your test code and it looks like it's working to me.
Here's what I get when I run the code:

https://skitch.com/bobsilverberg/g6xc4/localhost-vtisobjecttest-test.cfm-cfc1

Is that not what you see?

Bob

Robert Rawlins

unread,
Feb 3, 2012, 2:03:58 PM2/3/12
to valida...@googlegroups.com
Hi Bob,

This is what I see too - however, it's not quite what I would expect to see. 

Within the collection: Object 1 has 1 invalid property.
Object 2 has 2 invalid properties

Therefor I would expect to see 3 failures, not 2. What you're seeing is just the 2 failures for object 2.

I have been playing around with a custom version of the rule this afternoon and have come up with this, the implementation is probably horrible as I've never written a rule before, but it's a proof of concept:


This gives me the 3 failures as I would expect - furthermore it alters the 'objectType' of the failure and appends the index
of the array that the particular object being tested can be found at.

i.e.

checkin-request[1]
checkin-request[2]

So I can identify for which instance in the collection the failure relates when outputting errors.

Robert

Bob Silverberg

unread,
Feb 6, 2012, 2:29:27 PM2/6/12
to valida...@googlegroups.com
Hi Rob,

Thanks for taking the time to come up with a solution. I looked at it
today and it turned out that there was actually a one-line solution,
so I've implemented that. Please try it again using the attached file
and let me know if it works for you now.

Thanks,
Bob

ServerRuleValidator_IsValidObject.cfc

Bob Silverberg

unread,
Feb 6, 2012, 2:32:59 PM2/6/12
to valida...@googlegroups.com
Sorry, one more thing. I didn't implement your method of using the
object type to label each object because that information is already
available. Each struct in the array contains the actual object that
caused the failure, so you can interrogate that object however you see
fit to determine which instance it is. That way you van use the
actual data of the object, as opposed to just its index in the array.

Let me know if you can get that scheme working for you.

Thanks,
Bob

--
Bob Silverberg
www.silverwareconsulting.com

Rawlins

unread,
Feb 28, 2012, 5:29:40 AM2/28/12
to valida...@googlegroups.com
Hi Bob,

Dragging this issue back into the lime-light today. :-)

I've tried your code and in principle it appears to be working - I now get the full collection of failures for all objects within the result set.

However - I'm still trying to pull them out in a logical pattern so I can display the errors next to the appropriate form fields.

As you mention, a reference to the object is attached to the result set - however, I don't know how to group the failures for their logical object, like I can for instance with client field name.

My first thought was to use the injectResultIntoBO flag - however, this appears to inject all errors into every object in the collection, rather than just that objects specific failures.

Can you offer any advice / suggestions?

Thanks.

Robert


>>
>> For more options, visit this group at
>>
>> http://groups.google.com/group/validatethis?hl=en.
>>
>>
>>
>>
>> On 3 Feb 2012, at 12:24, Bob Silverberg wrote:
>>
>>
>> That's not the way it's supposed to work. Would it be possible for you to
>>
>> send me some code for an isolated test?
>>
>>
>> Thanks,
>>
>> Bob
>>
>>
>> Sent from my mobile
>>
>>
>> On 2012-02-03, at 5:46 AM, Rawlins <rob...@supporttime.com> wrote:
>>
>>
>> Hi Bob,
>>
>>
>> I can do this - however I get some slightly unexpected behaviour.
>>
>>
>> It appears to only return failures for the last object in the array, rather
>>
>> than every object.
>>
>>
>> So if I have an array of 3 invalid objects - only failures for the last
>>
>> invalid object are added to the result.
>>
>>
>> Thanks.
>>
>>
>> Robert
>>
>>
>> --
>>
>> You received this message because you are subscribed to the Google Groups
>>
>> "ValidateThis" group.
>>
>> To view this discussion on the web visit
>>
>> https://groups.google.com/d/msg/validatethis/-/0abzj2kiAh0J.
>>
>> To post to this group, send email to valida...@googlegroups.com.
>>
>> To unsubscribe from this group, send email to
>>


>>
>> For more options, visit this group at
>>
>> http://groups.google.com/group/validatethis?hl=en.
>>
>>
>>
>> --
>>
>> You received this message because you are subscribed to the Google Groups
>>
>> "ValidateThis" group.
>>
>> To post to this group, send email to valida...@googlegroups.com.
>>
>> To unsubscribe from this group, send email to
>>


>>
>> For more options, visit this group at
>>
>> http://groups.google.com/group/validatethis?hl=en.
>>
>>
>>
>>
>>
>>
>>
>> --
>>
>> Bob Silverberg
>>
>> www.silverwareconsulting.com
>>
>>
>> --
>>
>> You received this message because you are subscribed to the Google Groups
>> "ValidateThis" group.
>>
>> To post to this group, send email to valida...@googlegroups.com.
>>
>> To unsubscribe from this group, send email to


>>
>> For more options, visit this group at
>> http://groups.google.com/group/validatethis?hl=en.
>>
>>
>>
>> --
>>
>> You received this message because you are subscribed to the Google Groups
>> "ValidateThis" group.
>>
>> To post to this group, send email to valida...@googlegroups.com.
>>
>> To unsubscribe from this group, send email to


>>
>> For more options, visit this group at
>> http://groups.google.com/group/validatethis?hl=en.
>>
>>
>>
>>
>>
>> --
>> Bob Silverberg
>> www.silverwareconsulting.com
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "ValidateThis" group.
>> To post to this group, send email to valida...@googlegroups.com.
>> To unsubscribe from this group, send email to


>> For more options, visit this group at
>> http://groups.google.com/group/validatethis?hl=en.
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "ValidateThis" group.
>> To post to this group, send email to valida...@googlegroups.com.
>> To unsubscribe from this group, send email to


>> For more options, visit this group at
>> http://groups.google.com/group/validatethis?hl=en.
>
>
>
> --
> Bob Silverberg
> www.silverwareconsulting.com

Reply all
Reply to author
Forward
0 new messages