Forum: Ruby on Rails Generic validation / maxlength off column[colname].limit ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-03-02 21:28
(Received via mailing list)
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
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-03-02 21:42
(Received via mailing list)
> 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...
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-03-02 21:50
(Received via mailing list)
>> 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/client_side...
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-03-02 22:16
(Received via mailing list)
Hi Philip,

Philip Hallstrom wrote:

> Found them.
>
> http://agilewebdevelopment.com/plugins/enforce_schema_rules
> http://agilewebdevelopment.com/plugins/client_side...


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

Thanks!
Bill
This topic is locked and can not be replied to.