1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Database constraints are useful if you have multiple apps writing into database. As @greg mentioned is better to use your web framework for form validation. The database validation should be as last resort.
Thanks, I thought that it would be pretty clever to move as many logic as possible into database, but you're right that forms usually need quite different validation than data itself. There are many apps writing into our database, so constraints are really needed nevertheless.
@PeterKrejci true ! I use to move in the database only logic that does not change over time (slugification, authentication, data ordering...)
I have written a blog post about this question with a short example on how to use Form and Validator: pomm.coolkeums.org/news/use-form-with-pomm-in-silex.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.