rules derived from database definition

Hello,

what do you think of rules defaults which are

derived from the database definition?

I.e. unless you define other rules for these fields,

the db introspection would set them, e.g. not null->required.

I mean implicit defaults, not scaffolded code.

Example:

My database table contains a field name varchar(20) not null;

if i do not specifiy any rules for that field (or maybe

use something like 'name'=>'introspection', it will get a

required validator and a maximum length of 20.

But if i specify a validator manually, it will override that default.

Not sure this is  the good way to go, but the yiic tool already generates automatically rules based on db definition. I think this approach is clearer and more efficient.

I think mh has a good point here. One of the framework goals should be: Do not repeat yourself. If you have to change the length of a text field (i.e. varchar(255) to varchar(50)) then you currently have to change that in 3 places: in the database, in the rules of the model and in the view. It should be enough to change it in the database. If NOT NULL is set for a field in the DB it should automatically be considered as "required". The rules method can then be used to override the standard rules derived from the database.

The model already has all the the necessary information in the CMysqlColumnSchema objects. The CHtml methods could also use that information to automatically limit a text field.

I don't like this idea.  This might cause rules to be put into place that I do not expect.

For instance, I may have an attribute that has a max length of 100 in the database, but should have a larger max of 200 in the application.  The reason for this might be if for instance the field is parsed in some way (that shrinks the size) before being saved.

This will give a lot of people unexpected rules.

Currently you have the same problem when you use scaffolding, the database would say length 100 and the model rule as well. Then you would change that model rule to length 200. With the new approach there would initially be no model rule, but you would add that rule stating length 200. Its just less code for the standard cases.