Jump to content


Photo

[OOP] Three tier system - Where to check form input?


  • Please log in to reply
3 replies to this topic

#1 rubenski

rubenski
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 August 2006 - 12:28 AM

Hi folks. I am creating a three tiered system with a HTML front end and a mysql DB. I want to create a simple application that will store and retrieve information about rooms that are for rent (area, location, description, price_per_month, etc).

So far I have implemented

- a simple data class that connects with MYSQL and fires insert and update queries. (class RoomData)
- a business logic class that manages room fields (class Room)
- a HTML view containing a form that allows the user to enter a new room into the database.

Posted Image

Now, what happens when someone presses the submit bitton on the form? Obviously, the application must validate the form data first. This is where I get stuck, OO design-wise.

Where should I put the form validation code? In the Room class? Or would it be wiser to create a separate input validation class that extends Room?

What is your design strategy for input validation?




#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 August 2006 - 12:49 AM

while i can't comment on where to put your validation within the objects, i'll tell you how i do mine.  i use this approach in a non-OO way, but i think the process lends itself well to its own class.

i have a set of functions to validate various input types (email, phone, username, password, required (ie. not empty), etc.).  when i get my data, i run the functions as necessary and store all results within an array.  each function returns either 0 (int) or an error message, so when i check for errors i simply run a difference against 0 to pull out all the error strings.  if the resulting array is empty, then there were no errors, but otherwise i return the errors and exit the overall function.  this gives me a standardized validation method, albeit non-OO.

as i said earlier, i'll guess that a validation class is in order.  you'll be running input validation fairly integrally in any application, and so it's probably easiest to have it bundled together and just extend any overall application class with it.

maybe i'm wrong here, as i don't know the technicals ins and outs of good OO design.  just seems the most logical.

#3 rubenski

rubenski
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 13 August 2006 - 01:09 AM

as i said earlier, i'll guess that a validation class is in order.  you'll be running input validation fairly integrally in any application, and so it's probably easiest to have it bundled together and just extend any overall application class with it.


This is basically what I thought too. However, when I think of it again, it doesn't seem right. If I create other classes like ehhhm.... 'Member', 'Appointment', etc I'd have to create a separate Validator class to extend each of them. These separate validator classes would all contain nearly the same code. It sounds logical to me to create a separate validator class that contains all validation logic and that can be used by all application classes....

You think it would be wise to put the Validator class on top and let any application class (Room, Member, Appointment) extend it?

#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 August 2006 - 01:12 AM

i'm actually not clear on the idea of extending a class with another.  what i intended to say was it's probably easiest just to create a validation class that you can use from within any other class, since it will be standard regardless of the application accessing it.  not sure how to achieve that though, since i don't use OOP (nor do i know the technical term for it).




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users