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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
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
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> 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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sat, Feb 20, 2010 at 4:35 AM, Aldo Italo <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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:> #prevent users to write text uppercase, and reset field text to lowercaseDo you really need to "prevent" them, or can you just downcase it and be done? In any case, wouldn''t the easiest check be something like unless ( field.downcase == field ) errors.add( ... ) ? -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
i have resolved with this:
#prevent users to write text uppercase, and reset field text to
lowercase
validates_each :title, :description do |model, attr, value|
size_letters = value.scan(/\w/).size
size_invalid = value.scan(/[A-Z]/).size
if size_invalid > 0
if (size_letters / size_invalid) < 4 and size_letters > 10
model.errors.add( attr,
I18n.t(''activerecord.errors.messages.uppercase''))
model.write_attribute(attr, value.downcase)
end
end
end
for Hassan Schroeder: i want the user write in uppercae only certain
letters( itials of names, letter after points, etc..). with my solution
i concur the user to write in the text the 25% of letters uppercase.
thanks to all.
--
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.