tags:

views:

113

answers:

3

As a programmer, I love developing algorithms. I love to take a problem and work out a clean, efficient, readable, elegant solution. I seem to find, however, that the majority of my time is spent validating and cleaning form data, and passing it along to prepare various SQL statements. Perhaps this is "just the way it is" but I suspect I may be doing it wrong.

What do you do to avoid the deathtrap of endlessly validating input and building database interactions? Do you use a third-party library? Write your own library? Or is that simply the way it is?

+8  A: 

Most people abstract out a lot of the form validation and database transactions using some kind of framework. One example is the Zend framework Forms. I've seen people write their own libraries to add similar functionality.

For example, I've written my own simple library for smaller clients as well (just making each form element it's own class with a Basic "element" base class), a set of validation classes, and a Form class to wrap the elements. The form class calls the validate methods for each element and turns their data into an array that can be fed to a database class.

Your best bet is to figure out the needs for the websites and forms you are building. (Complex forms may be too difficult to abstract out). But simple forms could have their coding process stream lined without much difficulty.

GWW
Yes. Or CakePHP, or CodeIgniter, or Kohana, or Yii, etc. There's a fairly comprehensive list at http://www.phpframeworks.com/
stevelove
+1  A: 

I use Drupal for that, but you may prefer using some framework.

Yorirou
+2  A: 

If you're not using a framework (or can't), then you should definitely check out the php Filter methods: http://php.net/manual/en/book.filter.php

They're built in as of PHP 5.2

I've yet to see a good DB extraction outside of a framework, but the PDO abstraction layer is a good start ( http://php.net/manual/en/book.pdo.php )

P.S. I use Drupal too, when I can. The webform module in particular makes all this ridiculously easy.

Horatio Alderaan