Question

We are migrating to postgresql database and we want to use as many constraint checks as possible. The problem is when I want to validate web forms there is a lot of duplicate code which is hard to maintain. Is it possible to use this database checks also for web forms validation? The application is written in PHP and the database layer is pomm. The goal is just to grab all filled values, send it to database and get all failed constraints without defining additional rules in PHP application. How can I achive that the simplest way?

Was it helpful?

Solution

Well, database constraints are here to ensure your data are stored in a consistent manner. Form validation might have different rules depending on the functionnal context. Imho it is not a good idea to rely on database constraints to validate forms (I use the Symfony validator component for that).

From a purely technical point of view, if you want to use the database as validation layer, the hardest part is to extract why data could not be inserted / updated. Even if you could get this information from the Exception message, this will validate fields one by one until all fields are ok.

If you still want to go with it, catch the Pomm\Exception thrown and get the SQL error code from it.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top