Php – How do the popular PHP frameworks handle Form Input Errors

formsPHPvalidationzend-framework

I know for example the Zend Framework has some capability for creating form elements with validators. But now, lets say the user enters complete garbage data, which is invalid. What would happen next? Lets say the JS part goes wrong or JS is disabled, and the server receives the garbage data.

How do the "big" PHP frameworks handle this, conceptually? Please mention the framework and describe it, if you know. That would help a lot.

How are the error messages returned to the form and how are they displayed? How is that done technically?

From my point of view, in theory it has to go this way:

  1. User enters Garbage
  2. User submits the Form (JS validation fails, JS deactivated, whatever)
  3. PHP script receives garbage input
  4. PHP script validates garbage input on server side. All fields = FALSE, GARBAGE.
  5. Every time a field validation fails, the PHP script writes an input error message into an array.
  6. Error Message array is a map (associative array) where the key is the form element name
  7. Script loads the form again because of input errors
  8. Form script has all the logic to display the field input errors from the Error Message array
  9. User sees beautiful error messages and re-enters garbage.
  10. Over and over again.
  11. Until everything is all right > script saves the data and displays big THANKS message.

I know of no other, but if there is, I need to know 😉

Something tells me this is not the best solution.

Best Answer

Well first, unless you're using Dojo enabled forms the Zend Framework has nothing to do with javascript. JS validation would either be written by yourself, or tied into your Dojo enabled form.

Validation consists of iterating over each ZFE and checking it's Validator. If an error occurs, it is stored in the ZFE as an error message. When you display the form again, if you are using the default decorators, then you will have the Zend_Form_Decorator_Errors loaded, which "decorates" the element with it's error message.

I'd suggest reading the source code of Zend_Form starting with the isValid() method. It's very simple to read and see what's going on.