Custom validation methods

I’ve been working on extending AR validations with some custom
methods with the goals to:

a) reduce the bulk the standard syntax can result in
b) avoid having to write and read regex for common white-listed
character sets
c) allow some UTF-8 characters
d) provide a mechanism for making dynamic error messages

I’ve written a detailed article how I did it, and I’ve posted the
source I generated to be used or just looked at as an example. Thanks
to everyone that answered the questions that got me to this point.

http://www.railsdev.ws/blog/11/custom-validations-in-rails/

I reduced this application code (which does not allow for UTF-8 chars):

validates_format_of
:userFltrName,
:with => /^[a-zA-Z0-9_-\ ]*?$/,
:message => ‘accepts only letters, 0-9, underscore, hyphen, and
space’

validates_format_of
:userFltrTable,
:with => /^[a-zA-Z0-9_]*?$/,
:message => ‘accepts only letters, 0-9, and underscore’

validates_format_of
:userFltrField,
:with => /^[a-zA-Z0-9_]*?$/,
:message => ‘accepts only letters, 0-9, and underscore’

validates_inclusion_of
:userFltrOp,
:in => %w{eq bw ew cn lte lt gte gt btw},
:message => ‘has a value other than the valid options below’

down to the following replacement (which does allow UTF-8 chars):

validates_as_alpha_numeric_separator :userFltrName
validates_as_alpha_numeric_underscore :userFltrTable, :userFltrField
validates_as_value_list :userFltrOp, :in => %w{eq bw
ew cn lte lt gte gt btw}

So far, I have validators for:

validates_as_person_name
validates_as_business_name
validates_as_street_address
validates_as_alpha
validates_as_alpha_space
validates_as_alpha_hyphen
validates_as_alpha_underscore
validates_as_alpha_symbol
validates_as_alpha_separator
validates_as_alpha_numeric
validates_as_alpha_numeric_space
validates_as_alpha_numeric_hyphen
validates_as_alpha_numeric_underscore
validates_as_alpha_numeric_symbol
validates_as_alpha_numeric_separator
validates_as_numeric
validates_as_decimal
validates_as_positive_decimal
validates_as_integer
validates_as_positive_integer
validates_as_email
validates_as_value_list


def gw
acts_as_n00b
writes_at(www.railsdev.ws)
end

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