mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
Patterns should be activated on demand.
This commit is contained in:
parent
3387cc7066
commit
ff646641ed
2 changed files with 23 additions and 9 deletions
|
@ -5,16 +5,19 @@ module SimpleForm
|
||||||
private
|
private
|
||||||
|
|
||||||
def add_pattern!
|
def add_pattern!
|
||||||
input_html_options[:pattern] ||= pattern_source if validate_pattern?
|
input_html_options[:pattern] ||= pattern_source if options[:pattern]
|
||||||
end
|
|
||||||
|
|
||||||
def validate_pattern?
|
|
||||||
has_validators? && SimpleForm.html5 &&
|
|
||||||
SimpleForm.browser_validations && pattern_validator.present?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def pattern_source
|
def pattern_source
|
||||||
|
if options[:pattern] == true
|
||||||
|
if has_validators? && pattern_validator
|
||||||
pattern_validator.options[:with].source
|
pattern_validator.options[:with].source
|
||||||
|
else
|
||||||
|
raise "Could not find format validator for #{attribute_name}"
|
||||||
|
end
|
||||||
|
else
|
||||||
|
options[:pattern]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def pattern_validator
|
def pattern_validator
|
||||||
|
|
|
@ -335,11 +335,22 @@ class InputTest < ActionView::TestCase
|
||||||
assert_select 'input[max=119]'
|
assert_select 'input[max=119]'
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'input should infer pattern from attributes when it is present' do
|
test 'input should infer pattern from attributes when pattern is true' do
|
||||||
with_input_for @other_validating_user, :country, :string
|
with_input_for @other_validating_user, :country, :string, :pattern => true
|
||||||
assert_select 'input[pattern="\w+"]'
|
assert_select 'input[pattern="\w+"]'
|
||||||
end
|
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
|
test 'input should have step value of any except for integer attribute' do
|
||||||
with_input_for @validating_user, :age, :float
|
with_input_for @validating_user, :age, :float
|
||||||
assert_select 'input[step="any"]'
|
assert_select 'input[step="any"]'
|
||||||
|
|
Loading…
Reference in a new issue