Personally I'd use callbacks to take care of cleaning up your data
before it's passed to validate. #validate is really just for
validation and shouldn't manipulate your data. There are callbacks
that run pre-validation and they're best for what you're talking
about. Then your validations make sure everything is good to go before
shoving it into the db.
Kev
On 8/23/06, Tom Werner <[EMAIL PROTECTED]> wrote:
You can also create writers for your fields and do the cleanup there:
class Phone < ActiveRecord::Base
def number=(num)
write_attribute('number', num.gsub(/\D/, ''))
end
end
which would strip all non digits from a phone number when it's set.
Then, to double-make-sure you can add a validates_format_of to check it
again.
Tom
Patrick Crowley wrote:
> So I'm working on a site where users submit links that have certain
> properties.
>
> I'm using a couple of the standard validate helpers (presence_of,
> uniqueness_of, etc.) but there's additional content shaping I'd like
> to do.
>
> So far, I've stuffed these additional content filters into my model's
> validate method. Things like stripping white space, using pattern
> matching to reformat malformed links, etc.
>
> Just curious... is this common practice? I thought about putting this
> code in my controller, but it seems more appropriate to continually
> validate the link format, regardless of where content is entered into
> the system.
>
> -- Patrick
>
> _______________________________________________
> Sdruby mailing list
> [email protected]
> http://lists.sdruby.com/mailman/listinfo/sdruby
>
--
Tom Werner
Helmets to Hardhats
Software Developer
[EMAIL PROTECTED]
www.helmetstohardhats.org
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby
--
Kevin Clark
http://glu.ttono.us
_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby