Permalink
Browse files

Patterns should be activated on demand.

  • Loading branch information...
1 parent 3387cc7 commit ff646641edd481ea1635bd17a2b0e1467f825e92 @josevalim josevalim committed Sep 3, 2011
Showing with 23 additions and 9 deletions.
  1. +10 −7 lib/simple_form/helpers/pattern.rb
  2. +13 −2 test/inputs_test.rb
@@ -5,16 +5,19 @@ module Pattern #:nodoc:
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
@@ -335,11 +335,22 @@ def with_input_for(object, attribute_name, type, options={})
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"]'

0 comments on commit ff64664

Please sign in to comment.