Add case_sensitive option for confirmation validation
Case :- 1. In case of email confirmation one needs case insensitive comparison 2. In case of password confirmation one needs case sensitive comparison [ci skip] Update Guides for case_sensitive option in confirmation validation
This commit is contained in:
parent
40e904df37
commit
2438a1cf4e
|
@ -58,4 +58,10 @@
|
||||||
|
|
||||||
*Henrik Nyh*
|
*Henrik Nyh*
|
||||||
|
|
||||||
|
* Add case_sensitive option for confirmation validator in models.
|
||||||
|
|
||||||
|
See #17351
|
||||||
|
|
||||||
|
*Akshat Sharma*
|
||||||
|
|
||||||
Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/activemodel/CHANGELOG.md) for previous changes.
|
Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/activemodel/CHANGELOG.md) for previous changes.
|
||||||
|
|
|
@ -3,16 +3,18 @@ module ActiveModel
|
||||||
module Validations
|
module Validations
|
||||||
class ConfirmationValidator < EachValidator # :nodoc:
|
class ConfirmationValidator < EachValidator # :nodoc:
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
super
|
super({ case_sensitive: true }.merge!(options))
|
||||||
setup!(options[:class])
|
setup!(options[:class])
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_each(record, attribute, value)
|
def validate_each(record, attribute, value)
|
||||||
if (confirmed = record.send("#{attribute}_confirmation")) && (value != confirmed)
|
if (confirmed = record.send("#{attribute}_confirmation"))
|
||||||
|
unless confimation_value_equal?(record, attribute, value, confirmed)
|
||||||
human_attribute_name = record.class.human_attribute_name(attribute)
|
human_attribute_name = record.class.human_attribute_name(attribute)
|
||||||
record.errors.add(:"#{attribute}_confirmation", :confirmation, options.merge(attribute: human_attribute_name))
|
record.errors.add(:"#{attribute}_confirmation", :confirmation, options.merge(attribute: human_attribute_name))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def setup!(klass)
|
def setup!(klass)
|
||||||
|
@ -24,6 +26,14 @@ module ActiveModel
|
||||||
:"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation=")
|
:"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation=")
|
||||||
end.compact)
|
end.compact)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def confimation_value_equal?(record, attribute, value, confirmed)
|
||||||
|
if !options[:case_sensitive] && value.is_a? String
|
||||||
|
value.casecmp(confirmed) == 0
|
||||||
|
else
|
||||||
|
value == confirmed
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module HelperMethods
|
module HelperMethods
|
||||||
|
@ -55,6 +65,8 @@ module ActiveModel
|
||||||
# Configuration options:
|
# Configuration options:
|
||||||
# * <tt>:message</tt> - A custom error message (default is: "doesn't match
|
# * <tt>:message</tt> - A custom error message (default is: "doesn't match
|
||||||
# <tt>%{translated_attribute_name}</tt>").
|
# <tt>%{translated_attribute_name}</tt>").
|
||||||
|
# * <tt>:case_sensitive</tt> - Looks for an exact match. Ignored by
|
||||||
|
# non-text columns (+true+ by default).
|
||||||
#
|
#
|
||||||
# There is also a list of default options supported by every validator:
|
# There is also a list of default options supported by every validator:
|
||||||
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
# +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
|
||||||
|
|
|
@ -327,6 +327,16 @@ class Person < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
There is also a `:case_sensitive` option that you can use to define whether the
|
||||||
|
confirmation constraint will be case sensitive or not. This option defaults to
|
||||||
|
true.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class Person < ActiveRecord::Base
|
||||||
|
validates :email, confirmation: { case_sensitive: false }
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
The default error message for this helper is _"doesn't match confirmation"_.
|
The default error message for this helper is _"doesn't match confirmation"_.
|
||||||
|
|
||||||
### `exclusion`
|
### `exclusion`
|
||||||
|
|
Loading…
Reference in New Issue