Create custom validation

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?

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 I. [email protected]
wrote:

lowercase
end
end
validate detect_uppercase(title), detect_uppercase(description)
I18n.t(‘activerecord.errors.messages.uppercase’))
Posted via http://www.ruby-forum.com/.


Thanks:
Rajeev sharma

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 S.: 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.

On Sat, Feb 20, 2010 at 4:35 AM, Aldo I. [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:

#prevent users to write text uppercase, and reset field text to lowercase

Do 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 S. ------------------------ [email protected]
twitter: @hassan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs