Patterns should be activated on demand.

This commit is contained in:
José Valim 2011-09-03 12:11:15 +02:00
parent 3387cc7066
commit ff646641ed
2 changed files with 23 additions and 9 deletions

View File

@ -5,16 +5,19 @@ module SimpleForm
private
def add_pattern!
input_html_options[:pattern] ||= pattern_source if validate_pattern?
end
def validate_pattern?
has_validators? && SimpleForm.html5 &&
SimpleForm.browser_validations && pattern_validator.present?
input_html_options[:pattern] ||= pattern_source if options[:pattern]
end
def pattern_source
pattern_validator.options[:with].source
if options[:pattern] == true
if has_validators? && pattern_validator
pattern_validator.options[:with].source
else
raise "Could not find format validator for #{attribute_name}"
end
else
options[:pattern]
end
end
def pattern_validator

View File

@ -335,11 +335,22 @@ class InputTest < ActionView::TestCase
assert_select 'input[max=119]'
end
test 'input should infer pattern from attributes when it is present' do
with_input_for @other_validating_user, :country, :string
test 'input should infer pattern from attributes when pattern is true' do
with_input_for @other_validating_user, :country, :string, :pattern => true
assert_select 'input[pattern="\w+"]'
end
test 'input should use given pattern from attributes' do
with_input_for @other_validating_user, :country, :string, :pattern => "\\d+"
assert_select 'input[pattern="\d+"]'
end
test 'input should fail if pattern is true but no pattern exists' do
assert_raise RuntimeError do
with_input_for @other_validating_user, :name, :string, :pattern => true
end
end
test 'input should have step value of any except for integer attribute' do
with_input_for @validating_user, :age, :float
assert_select 'input[step="any"]'