PDA

View Full Version : One command object, x validation defs?


syg6
Aug 14th, 2007, 05:12 AM
I have a couple objects, Company and Employee. CRUD of these objects works fine. But there is a 1:n relationship between Company and Employee. So on my Company 'form' page I have all the data for the Company, as well as a list of Employees. I need to be able to add Employees to and delete Employees from my Company, as well as be able to edit any Employee.

What I am confused about is how to make validation work in one way when I save or update a company (make sure required fields are filled in correctly, make sure at least one Employee is assigned) and to work in another way when I assign an Employee to a Company (only make sure an Employee is selected). When I assign an Employee I would do it in a different page or a pop-up window, with a list of potential Employees, but with the same Command Object, Company.

Since I define my validation for Company in validation.xml, validation would fail since all the rest of the fields are null, since they are not present.

So how do I tell Spring to sometimes validate one way, sometimes another?

Thanks!
Bob

pmularien
Aug 14th, 2007, 01:26 PM
Assuming your controller is a subclass of BaseCommandController (pretty likely), I would look into overriding bindAndValidate - you'd want the default superclass behavior in the case where you're doing the standard validation, and for the add/delete employee case you cite, you'd handle that with some custom logic in your version of this method.

Does that make sense?

syg6
Aug 16th, 2007, 07:48 AM
Hello, thanks for the reply. (I had a couple days vk).

Yes, my Controller is of the BaseCommandController flavour. Your solutions seems to be what I need to do. I've re-worked the app a bit so it might not be necessary, at least not for this particular page, but I am sure we'll have to use partial / custom validation at some point, at which time I'll try to override the bindAndValidate method as you suggested.

Many thanks!
Bob