Try this
def validates_on_yours_method_name
size_letters = field.scan(/\w/).size
size_invalid = field.scan(/[A-Z]/).size
if size_invalid > 0
if (size_letters / size_invalid) < 4 and size_letters > 10
errors.add( field,
I18n.t('activerecord.errors.
messages.uppercase'))
write_attribute(field, field.downcase)
end
end
end
On Sat, Feb 20, 2010 at 6:05 PM, Aldo Italo <[email protected]> wrote:
> hi, i created a validation to prevent users to write text uppercase in
> my fields, i have writing the code on my class model like this:
>
> #####################
>
> validate :detect_uppercase_title, :detect_uppercase_description
>
> private
>
> #prevent users to write text uppercase, and reset field text to
> lowercase
> def detect_uppercase_title
> size_letters = title.scan(/\w/).size
> size_invalid = title.scan(/[A-Z]/).size
> if size_invalid > 0
> if (size_letters / size_invalid) < 4 and size_letters > 10
> errors.add( :title,
> I18n.t('activerecord.errors.messages.uppercase'))
> write_attribute(:title, title.downcase)
> end
> end
> end
> def detect_uppercase_description
> size_letters = description.scan(/\w/).size
> size_invalid = description.scan(/[A-Z]/).size
> if size_invalid > 0
> if (size_letters / size_invalid) < 4 and size_letters > 10
> errors.add( :description,
> I18n.t('activerecord.errors.messages.uppercase'))
> write_attribute(:description, description.downcase)
> end
> end
> end
>
> #####################
>
> but now i want to define a function to doing this without repeating and
> make more DRY the code.
>
> i have tried with this:
>
> #####################
>
> validate detect_uppercase(title), detect_uppercase(description)
>
> private
>
>
> def detect_uppercase(field)
> size_letters = field.scan(/\w/).size
> size_invalid = field.scan(/[A-Z]/).size
> if size_invalid > 0
> if (size_letters / size_invalid) < 4 and size_letters > 10
> errors.add( field,
> I18n.t('activerecord.errors.messages.uppercase'))
> write_attribute(field, field.downcase)
> end
> end
> end
>
> #####################
>
> but i see it not work.
> hw can i do to implement my necessity?
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rubyonrails-talk%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
Thanks:
Rajeev sharma
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.