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

**URL:** <https://www.ruby-forum.com/t/generic-validation-maxlength-off-column-colname-limit/87477>\
**Category:** Rails\
**Created:** [March 2, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/generic-validation-maxlength-off-column-colname-limit/87477 "2007-03-02T21:28:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 2, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/generic-validation-maxlength-off-column-colname-limit/87477/1 "2007-03-02T21:28:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 2, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/generic-validation-maxlength-off-column-colname-limit/87477/2 "2007-03-02T21:42:29Z")

</div>

> Second is that, in the model, I’ve got to list each attribute I want to
> 
> 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…

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 2, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/generic-validation-maxlength-off-column-colname-limit/87477/3 "2007-03-02T21:50:29Z")

</div>

> > Second is that, in the model, I’ve got to list each attribute I want to
> > 
> > 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/enforce_schema_rules)  
[http://agilewebdevelopment.com/plugins/client\_side\_validation](http://agilewebdevelopment.com/plugins/client_side_validation)

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [March 2, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/generic-validation-maxlength-off-column-colname-limit/87477/4 "2007-03-02T22:16:36Z")

</div>

Hi Philip,

Philip H. wrote:

> Found them.
> 
> [http://agilewebdevelopment.com/plugins/enforce\_schema\_rules](http://agilewebdevelopment.com/plugins/enforce_schema_rules)  
> [http://agilewebdevelopment.com/plugins/client\_side\_validation](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
