j4mie / paris

A lightweight Active Record implementation for PHP5, built on top of Idiorm.
http://j4mie.github.com/idiormandparis/
996 stars 131 forks source link

Validation? #2

Closed jordanlev closed 13 years ago

jordanlev commented 14 years ago

Will validation be implemented? There are a couple of different kinds: built-in automatic rules based on database schema (e.g. required values for "not null" fields, max lengths for VARCHAR sizes, integers for INT fields, etc.), and also systems for adding your own rules via callbacks.

jordanlev commented 14 years ago

j4mie mentioned via email that built-in validations (based on database schema rules) were NOT on the roadmap because idiorm/paris don't do any database introspection. Other validation rules are probably too heavyweight as well for this library -- you can just create your own validation method in each model.

jordanlev commented 14 years ago

FWIW, I agree that if database introspection isn't already in the library, it's not worth adding it just for this.

j4mie commented 13 years ago

After much thought, I have decided to not add explicit support for validation to Paris. The requirements for each application are so different that trying to add generic support would end up far too complex.

It's pretty trivial to add basic validation to your models by overriding the save method, checking the data is valid, and returning false if it is not. It would also be simple to subclass the Model base class and add your own generic validation methods.

jordanlev commented 13 years ago

Makes sense to me (given the fact that no database introspection going on). You might want to consider adding a "Validation" section to the documentation and just putting that last paragraph in there -- then at least people have a recommended approach to it (anything to discourage model validation in form processing will help make the world a better place, IMHO)

j4mie commented 13 years ago

Good idea - I'll add that now.

As an aside, I don't think database introspection is very useful for validation anyway. You might be able to validate that a particular column must be a varchar, but that's as complex as you can get - the database can't tell you if it should be a valid email address or phone number.

jordanlev commented 13 years ago

re: introspection -- it's not so much about usefulness or completeness as it is about avoiding logic duplication. As long as you're setting a NOT NULL or VARCHAR length on a field in the database, you shouldn't have to declare "required" or "max length" separately in the code. Email addresses and phone number formats can't be represented in a database schema, so declaring those in code isn't duplicating anything.

(But this is just a "in a perfect world" aside -- I completely agree that it doesn't belong in paris.)

j4mie commented 13 years ago

Ok - added this to README in

SHA: 77adca77266e8489a3a92521f7b23539b63f2c4e