Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Struts validator - checking unique constraint

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Validator framework with Struts, I ran into this problem:
I have a Phone details page, on which you can do CRUD operations on phones. One of the requirements is that phone number attribute must be unique in the DB.
Now, my question is, how can one do it "properly" ? I mean all sources strongly suggested that the ActionForm (ValidatorForm) object should not have connection to database as the data-view transfer is made by DAO objects. But this'd mean that "bad" data can pass the validation stage, and also it escapes the Struts trap redirecting this bad data to the form itself.
Anyone, any suggestions ?

Thanks in advance,

Gyula
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Business Delegate Pattern - read this.

Keep in mind though that if it passes validation it might not still be valid when you try to submit it to the backend. Someone else may have been entering the same number at about the same time. Having a duplicate number might be better to not "validate" until you try to persist the data. That way, you make only one call to the backend and limit the risk of having a value pass validation but then not be valid when it is persisted.
[ November 05, 2004: Message edited by: Marc Peabody ]
 
Gyula Klinszky
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Marc Peabody:
Business Delegate Pattern - read this.

Keep in mind though that if it passes validation it might not still be valid when you try to submit it to the backend. Someone else may have been entering the same number at about the same time. Having a duplicate number might be better to not "validate" until you try to persist the data. That way, you make only one call to the backend and limit the risk of having a value pass validation but then not be valid when it is persisted.

[ November 05, 2004: Message edited by: Marc Peabody ]



Hi, Marc,

I ran through the pattern you recommended. I am using similar technique with DAO, and DAOFactory objects (e.g. persisting the Person object with createPerson(Person), and updatePerson(Person) calls).
You are right about the duplicate validation. What I failed to find so far though is a way to link back to validation, sending ActionError back, and reloading the form with the unsaved data as I already passed the validate() method of the form, and am in the execute() method of the Action.
 
Gyula Klinszky
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, no takers ?
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel the question is defined in 2 levels. The 1st level should be the data type itself. As it is a phone number, it should be composed of numbers and dash. This level aims to check for data type only, because Struts validation framework aims for such pattern matching.

The 2nd level should be the business type validation, which should be done in the business logic. In such sense, why you wanna bind to the frontend checking? Why dont you allow your business logic to check it with DB, and throw something like InvalidPhoneNumberException? So, the ActionDispatcher finally catch this exception, and execute the "fail" path?

Nick
 
Gyula Klinszky
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nicholas Cheung:
I feel the question is defined in 2 levels. The 1st level should be the data type itself. As it is a phone number, it should be composed of numbers and dash. This level aims to check for data type only, because Struts validation framework aims for such pattern matching.

The 2nd level should be the business type validation, which should be done in the business logic. In such sense, why you wanna bind to the frontend checking? Why dont you allow your business logic to check it with DB, and throw something like InvalidPhoneNumberException? So, the ActionDispatcher finally catch this exception, and execute the "fail" path?

Nick



Thank you, Nicholas, I got it solved as you suggested.
I was missing the obvious, that on the fail forward path you can link back to the same form jsp, and pass along error messages by using saveErrors() call in the Action.
My bad, seems so obvious now
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My bad, seems so obvious now


Dont mind, that great that we help.

Nick
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic