Generic validation / maxlength off column[colname].limit ?

I’m currently using validates_length_of to guard against buffer overruns in the model and using js max_length in the views to try to “head it off at the pass.” There are two problems with the approach I’m currently using.

First is that the length value is hardcoded in both the model and view so that if I change the length of the string in the database, I have to change the value in both the model validations and any view that uses the string.

Second is that, in the model, I’ve got to list each attribute I want to validate individually. Some of the models have a lot of attributes.

I’ve looked at using validates_each and that could help a little with the second problem, but it’s still unwieldly listing each attribute.

For the model side of this, I’m thinking of writing a generic validation that iterates through the attributes of a model object on a save or update and if it’s a string or char type, get it’s size / limit from the table column attributes and validate against that. That would seem to give me immunity in the validation from changes in the column sizes.

For the view side, I’m thinking of a similar approach that feed’s the maxlength with a value based, again, on the column size rather than a hard-coded value.

Two questions. Anybody know of something like that that already exists? Anybody think it’s not doable?

Thanks,

Bill

I'm currently using validates_length_of to guard against buffer overruns in the model and using js max_length in the views to try to "head it off at the pass." There are two problems with the approach I'm currently using.

First is that the length value is hardcoded in both the model and view so that if I change the length of the string in the database, I have to change the value in both the model validations and any view that uses the string.

Second is that, in the model, I've got to list each attribute I want to validate individually. Some of the models have a lot of attributes.

I've looked at using validates_each and that could help a little with the second problem, but it's still unwieldly listing each attribute.

For the model side of this, I'm thinking of writing a generic validation that iterates through the attributes of a model object on a save or update and if it's a string or char type, get it's size / limit from the table column attributes and validate against that. That would seem to give me immunity in the validation from changes in the column sizes.

For the view side, I'm thinking of a similar approach that feed's the maxlength with a value based, again, on the column size rather than a hard-coded value.

Two questions. Anybody know of something like that that already exists? Anybody think it's not doable?

I can't remember the name, but I've seen a plugin that will automatically turn your model validations into javascript validations... that would get you half way there...

I'm currently using validates_length_of to guard against buffer overruns in the model and using js max_length in the views to try to "head it off at the pass." There are two problems with the approach I'm currently using.

First is that the length value is hardcoded in both the model and view so that if I change the length of the string in the database, I have to change the value in both the model validations and any view that uses the string.

Second is that, in the model, I've got to list each attribute I want to validate individually. Some of the models have a lot of attributes.

I've looked at using validates_each and that could help a little with the second problem, but it's still unwieldly listing each attribute.

For the model side of this, I'm thinking of writing a generic validation that iterates through the attributes of a model object on a save or update and if it's a string or char type, get it's size / limit from the table column attributes and validate against that. That would seem to give me immunity in the validation from changes in the column sizes.

For the view side, I'm thinking of a similar approach that feed's the maxlength with a value based, again, on the column size rather than a hard-coded value.

Two questions. Anybody know of something like that that already exists? Anybody think it's not doable?

I can't remember the name, but I've seen a plugin that will automatically turn your model validations into javascript validations... that would get you half way there...

Found them.

http://agilewebdevelopment.com/plugins/enforce_schema_rules http://agilewebdevelopment.com/plugins/client_side_validation

Hi Philip,

Philip Hallstrom wrote:

Found them.

http://agilewebdevelopment.com/plugins/enforce_schema_rules http://agilewebdevelopment.com/plugins/client_side_validation

Super!!! Haven't had a look yet but, worst case, that's a big head start!

Thanks! Bill