Permalink
Browse files

Add TelephoneFormatValidator and use it for views

Add TelephoneFormatValidator mainly to be able to determine attributes that are phone numbers and to generate an appropriate HTML5 input element automatically.  The validator itself is not very important, could be left empty.
  • Loading branch information...
1 parent ffc6592 commit 8eee01e965ff0747ae543d50e8523b7c90cddad3 @alexeymuranov committed Nov 4, 2012
View
3 internationalisation/defaults/en.yml
@@ -128,7 +128,8 @@ en:
one: is the wrong length (should be 1 character)
other: is the wrong length (should be %{count} characters)
# Custom:
- email_format: "is not well-formed"
+ email_format: "does not look like a valid email address"
+ telephone_format: "does not look like a valid phone number"
template:
body: ! 'There were problems with the following fields:'
View
3 internationalisation/defaults/fr.yml
@@ -213,7 +213,8 @@ fr:
taken: "est déjà pris(e)"
record_invalid: "La validation a échoué : %{errors}"
# Custom:
- email_format: "n'est pas bien formée"
+ email_format: "ne semble pas être une vraie adresse email"
+ telephone_format: "ne semble pas être un vrai numéro de téléphone"
template: &errors_template
header:
View
7 lib/attribute_constraints.rb
@@ -1,5 +1,5 @@
require 'set'
-require './lib/email_format_validator' # custom validator
+require './lib/format_validators' # custom validator
module AttributeConstraints # TESTME
def self.included(base_class)
@@ -46,9 +46,10 @@ def initialize_attribute_constraints
v.is_a?(ActiveModel::Validations::InclusionValidator)
}
- if validator_classes_on(attr).include?(
- EmailFormatValidator)
+ if validator_classes_on(attr).include?(EmailFormatValidator)
h[:format] = :email
+ elsif validator_classes_on(attr).include?(TelephoneFormatValidator)
+ h[:format] = :telephone
end
inclusion_validator = validators_on(attr).find { |v|
View
8 lib/email_format_validator.rb
@@ -1,8 +0,0 @@
-class EmailFormatValidator < ActiveModel::EachValidator
- def validate_each(object, attribute, value)
- unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
- object.errors[attribute] <<
- (options[:message] || I18n.t('errors.messages.email_format'))
- end
- end
-end
View
17 lib/format_validators.rb
@@ -0,0 +1,17 @@
+class EmailFormatValidator < ActiveModel::EachValidator
+ def validate_each(object, attribute, value)
+ unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
+ object.errors[attribute] <<
+ (options[:message] || I18n.t('errors.messages.email_format'))
+ end
+ end
+end
+
+class TelephoneFormatValidator < ActiveModel::EachValidator
+ def validate_each(object, attribute, value)
+ unless value =~ /\A\s*\+?(\d+(\-|\ +))*\d+\s*\z/i
+ object.errors[attribute] <<
+ (options[:message] || I18n.t('errors.messages.telephone_format'))
+ end
+ end
+end
View
4 models.rb
@@ -4,7 +4,7 @@
require './lib/attribute_types'
require './lib/attribute_constraints'
-require './lib/email_format_validator'
+require './lib/format_validators'
# Internationalisation
# NOTE: normally should be used only in helpers or presenters
@@ -172,6 +172,8 @@ class Participant < AbstractSmarterModel
validates :email, :email_format => true
+ validates :phone, :telephone_format => true
+
validates :gender,
:inclusion => { :in => ['female', 'male', :female, :male] },
:allow_nil => true
View
2 presentation_helpers.rb
@@ -40,6 +40,8 @@ def attribute_input_html_type(model, attribute, column_type = nil)
case model.attribute_constraints_on(attribute)[:format]
when :email
:email
+ when :telephone
+ :tel
else
:text
end
View
2 view_templates/pages/_registration_form.haml
@@ -96,7 +96,7 @@
%dd.input
%input{ :id => "participant_#{ attr }",
:name => "participant[#{ attr }]",
- :type => :text,
+ :type => attribute_input_html_type(Participant, attr),
:value => @participant.public_send(attr) }
- [:i_m_t_member, :g_d_r_member, :invitation_needed, :visa_needed].each do |attr|

0 comments on commit 8eee01e

Please sign in to comment.