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!
Error Handling within MVC
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
I use Validator/Rule style, but I don't think it is as popular as monolithic validation classes like PEAR Validate or Zend_Filter_Input. I think Zend's form controller is going toward something more like PEAR's QuickForm from what I have seen. So maybe take a look at the PEAR classes for ideas.
(#10850)
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
You would validate from the UserController::CreateAction() method. The first term "user" specifies the controller (which is a separate file). The second "create" specifies the Action method within that controller class which will be called.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?
This isn't in your direction - but in general I see more than a few folk on these forums call sessions overkill. I find this a bit strange, since sessions are quite a fast and convenient method of storing user specific data temporarily. So sessions are one method. In this case, however you don't need to store anything between requests. Look at the workflow. The user has submitted a form, you have validated the form in the CreateAction method of your controller. At this point you can gather a list of all errors. One suggestion would be to set these up as variables in your UserRegister's view template. From CreateAction() set these variable values in the view. Now, is there any reason not to re-use the same template you had for RegisterAction() in CreateAction()? AFAIK, you can do this without any problems.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.
I suppose the main point here is that no state is required. Call the same template (the Registration form) from both actions. You can setup the template to check if any errors have been assigned to one of its variables (as an array, or multidimensional array if you wish to categorise the errors on a per-field basis for presentation). If so, the template can simply traverse the array and insert the relevant errors.
Of course, if no errors occur - you can just let the create action use the next expected template instead.