Validating Dynamic Multi Object Forms

17 views
Skip to first unread message

Rawlins

unread,
Jan 6, 2012, 9:57:51 AM1/6/12
to valida...@googlegroups.com
Hello Everyone,

Happy new year to you all.

I have a challenge which seems to be occurring on a regular basis within this application and I'm at a loss as to how I should effectively validate the data. I have a working solution, but it's pretty large and verbose hack.

I have a form, which is dynamically generated from a number of objects. An example of this might be something like so: http://cl.ly/2B1q2F1f2H180a2i3r1n

The markup for each row of the form would look something like this (simplified): http://pastebin.com/bTTGLL2t

Now, what I need to do is validate that for each item in the list that is checked, that a correct date was given, and also that a courier was selected.

Because this is a dynamic form which could have any number of rows with inputs of dynamic name / ID - I can't see a way of configuring / automating the validation.

At the moment I do this all quite manually, something like this: http://pastebin.com/iYVNtYLz

Within the loop I will also do the validation for the courier and any other values specific to that item, sometimes 4 or 5 rules.

This however is creating some pretty fat controller methods (this is where I perform the validation.) as it's so verbose and I'm really not happy with that.

Does anyone have similar challenges within their applications? or have suggestions on how I can streamline this process?

Thanks all. If you have any more information about the code etc then please let me know.

Robert

Bob Silverberg

unread,
Jan 6, 2012, 10:47:06 AM1/6/12
to valida...@googlegroups.com
Hi Robert,

Regardless of what you do to validate, you're still going to have to
have some logic to loop through the fields to extract the data, as
only your program knows how to interpret that form post, and as that
logic is based on the form I think it does make sense to have that in
a controller. You may want to refactor it out into a base controller
or a helper object which gets injected into the controller, but I do
think it belongs in the controller's domain.

As for doing the actual validation, how about something like this:
Inside the loop, create an instance of an object, populate it with the
values from the row, then pass it to VT to validate? You could also
look at using the isValidObject validator
(http://www.validatethis.org/docs/wiki/Validation_Types_Supported_By_ValidateThis.cfm#IsValidObject)
by creating a container object and then loading each of these single
row objects into it, and then passing the whole thing to VT.

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/-/loXX8JhNZb8J.
> 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,
Jan 6, 2012, 11:03:13 AM1/6/12
to valida...@googlegroups.com
Hi Bob,

Good call on the looping - I may look at building a customer populateModel() type method that deals with utterable collections based on some form of naming convention on the fields.

And as you're saying, this could effectively populate a custom object that represents the form, that could then employ it's own validation method.

Hmmm I like this ideas - may work up some simple tests and see if I can get something working.

What I really want to try and escape is having to build a custom collection, populator, object and validation rule set for each form, however, this might just not be viable.

Robert

Bob Silverberg

unread,
Jan 6, 2012, 11:24:10 AM1/6/12
to valida...@googlegroups.com
Check out isValidObject first. I don't see why you'd have to create a
custom validation method for each form. You should be able to just
create a definition file and allow VT to do most of the work. Of
course I'm saying that without knowing much about your validation
requirements.

Bob

--
Bob Silverberg
www.silverwareconsulting.com

Reply all
Reply to author
Forward
0 new messages