Validation of a screen of multiple domain objects

View: New views
4 Messages — Rating Filter:   Alert me  

Validation of a screen of multiple domain objects

by Martin Flower :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Complete Grails/Groovy newby here ...

My create screen has one Book per row (isbn, title).  I want to  
enforce unique isbn and unique name.  I can code this contraint in the  
Book class, but when I Book.findByIsbn(val), I am only searching on  
the persisted Books, not on those that have not yet been saved.  If I  
have the same Book multiple times on the create screen, but none of  
which are saved, then no error is detected by the contraints.  When I  
call save(), the first one is saved and the others are discarded.

The screen is defined as an array of Strings.  Should I be using a  
Command object - Books, or maybe Book[] ?

I have been playing around with passing the array of input parameters  
to the Book class for picking up by the contraints ....

I have been looking to put the validation in the BookController, but I  
can't work out how to attach the error condition to the Book so that  
it gets displayed on the screen.

I think I'm tying myself in knots ... time to ask for some help

Thanks

Martin


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Validation of a screen of multiple domain objects

by khelenek2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'll throw out some ideas. I'm not sure if this will exactly solve your problems, but..  I think you should create a service class to save all your books, since this is a good use case for a transaction.  If you save them one by one, any duplicates should fail to save() with a constraint error.  Then you can roll back the transaction* and have your controller redisplay the form (which will print all the errors on your book objects.) Also check out the unique constraint (looks like you coded your own?)

* i don't know how to rollback a transaction off the top of my head - someone else can jump in or you can research that..

On Wed, May 14, 2008 at 4:21 PM, Martin Flower <sylvestris@...> wrote:
Complete Grails/Groovy newby here ...

My create screen has one Book per row (isbn, title).  I want to enforce unique isbn and unique name.  I can code this contraint in the Book class, but when I Book.findByIsbn(val), I am only searching on the persisted Books, not on those that have not yet been saved.  If I have the same Book multiple times on the create screen, but none of which are saved, then no error is detected by the contraints.  When I call save(), the first one is saved and the others are discarded.

The screen is defined as an array of Strings.  Should I be using a Command object - Books, or maybe Book[] ?

I have been playing around with passing the array of input parameters to the Book class for picking up by the contraints ....

I have been looking to put the validation in the BookController, but I can't work out how to attach the error condition to the Book so that it gets displayed on the screen.

I think I'm tying myself in knots ... time to ask for some help

Thanks

Martin


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email




Re: Validation of a screen of multiple domain objects

by Peter Ledbrook-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> My create screen has one Book per row (isbn, title).  I want to enforce
> unique isbn and unique name.  I can code this contraint in the Book class,
> but when I Book.findByIsbn(val), I am only searching on the persisted Books,
> not on those that have not yet been saved.  If I have the same Book multiple
> times on the create screen, but none of which are saved, then no error is
> detected by the contraints.  When I call save(), the first one is saved and
> the others are discarded.

BTW, this kind of question should be directed to the user mailing
list. The added advantage is that more people read that list and can
help you :)

Cheers,

Peter

--
Software Engineer
G2One, Inc.
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Validation of a screen of multiple domain objects

by Martin Flower :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Peter - I think I had my mailing lists mixed up.  I do have a  
solution (of sorts), but I'm using this as an exercise in best  
practice - so I want to find the grooviest way.   I'll send a separate  
email to the user list and see what comments I get there.  Thanks.  
Martin

On 16 May 2008, at 12:19, Peter Ledbrook wrote:

> My create screen has one Book per row (isbn, title).  I want to  
> enforce
> unique isbn and unique name.  I can code this contraint in the Book  
> class,
> but when I Book.findByIsbn(val), I am only searching on the  
> persisted Books,
> not on those that have not yet been saved.  If I have the same Book  
> multiple
> times on the create screen, but none of which are saved, then no  
> error is
> detected by the contraints.  When I call save(), the first one is  
> saved and
> the others are discarded.

BTW, this kind of question should be directed to the user mailing
list. The added advantage is that more people read that list and can
help you :)

Cheers,

Peter

--
Software Engineer
G2One, Inc.
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email