Error Handling within MVC
Posted: Tue Oct 03, 2006 9:12 pm
This is my first time using MVC and I've decided to try out the Zend Framework. I'm in the design phase of a medium sized web application, and I'm having some difficulty braining through how some things connect due to my inexperience. I've created a number of views, and my controllers seem to work just fine. Interfacing with the database is easy, but I'm trying to create a relatively robust FormValidator class and a way to deliver input errors back to the user. That leads to my first question.
Is it appropriate to just instance the FormValidator once and reset it after it validates a single field or should I be creating a new instance for each field? My gut instinct says to instance it once for memory purposes, I was just curious if the process of resetting it would be expensive(it won't have many variables).
I want to be able to capture multiple errors for each field, so I was thinking of foregoing exceptions and try/catch. I'm not quite sure if I'm on the right track. Basically, I'm thinking of just doing regular expressions to determine if a field is a valid email or string or whatever and then if an error occurs, I will add a string to an error array. Would it be better if I were to create my own exceptions like InvalidEmailException and throw those and then handle them with some sort of Error Template type thing instead?
I also can't seem to figure out an elegant way to deliver the errors that are generated. For instance, if I have a URL like http://www.mydomain.com/user/register and the post action to create a new user is http://www.mydomain.com/user/create, I would validate the field from the register form in the create controller, correct? If so, how would I redisplay the register page with correct errors? I'd have to deliver them from create to register and I know PHP doesn't maintain state. I was thinking of using sessions, but that seems to be overkill. Should I just send the errors to the register page as post variables and run a zend filter on them to make sure they aren't tainted? Previously, I'd posted to the same page so /user/register would post to /user/register and look for its own post variables and then it would output its own errors if there were any. Is there any way to overcome this going from one URL to another? Thanks for any and all help!
Is it appropriate to just instance the FormValidator once and reset it after it validates a single field or should I be creating a new instance for each field? My gut instinct says to instance it once for memory purposes, I was just curious if the process of resetting it would be expensive(it won't have many variables).
I want to be able to capture multiple errors for each field, so I was thinking of foregoing exceptions and try/catch. I'm not quite sure if I'm on the right track. Basically, I'm thinking of just doing regular expressions to determine if a field is a valid email or string or whatever and then if an error occurs, I will add a string to an error array. Would it be better if I were to create my own exceptions like InvalidEmailException and throw those and then handle them with some sort of Error Template type thing instead?
I also can't seem to figure out an elegant way to deliver the errors that are generated. For instance, if I have a URL like http://www.mydomain.com/user/register and the post action to create a new user is http://www.mydomain.com/user/create, I would validate the field from the register form in the create controller, correct? If so, how would I redisplay the register page with correct errors? I'd have to deliver them from create to register and I know PHP doesn't maintain state. I was thinking of using sessions, but that seems to be overkill. Should I just send the errors to the register page as post variables and run a zend filter on them to make sure they aren't tainted? Previously, I'd posted to the same page so /user/register would post to /user/register and look for its own post variables and then it would output its own errors if there were any. Is there any way to overcome this going from one URL to another? Thanks for any and all help!