Skip to content

Commit

Permalink
Merge pull request #828 from AquisTech/bugfix-827-nested-form-breaks-…
Browse files Browse the repository at this point in the history
…for-fields

Bugfix #827 Fix nested forms `f.fields`
  • Loading branch information
tagliala committed Apr 16, 2021
2 parents 34959f1 + 46bb703 commit cc318bc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [ENHANCEMENT] Test against jQuery 3.6.0 by default
* [ENHANCEMENT] Test against latest Ruby versions
* [FEATURE] Add support to `fields` method ([#828](https://github.com/DavyJonesLocker/client_side_validations/pull/828))

## 18.0.0 / 2021-02-13

Expand Down
7 changes: 6 additions & 1 deletion lib/client_side_validations/action_view/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module ActionView
module Helpers
module FormBuilder
def self.prepended(base)
(base.field_helpers - %i[label check_box radio_button fields_for hidden_field file_field]).each do |selector|
(base.field_helpers - %i[label check_box radio_button fields_for fields hidden_field file_field]).each do |selector|
base.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
# Cannot call super here, rewrite all
def #{selector}(method, options = {}) # def text_field(method, options = {})
Expand Down Expand Up @@ -72,6 +72,11 @@ def fields_for(record_name, record_object = nil, fields_options = {}, &block)
super(record_name, record_object, fields_options, &block)
end

def fields(scope = nil, model: nil, **options, &block)
options[:validate] ||= @options[:validate] if @options[:validate] && !options.key?(:validate)
super(scope, model: model, **options, &block)
end

def file_field(method, options = {})
build_validation_options(method, options)
options.delete(:validate)
Expand Down
15 changes: 15 additions & 0 deletions test/action_view/cases/test_form_with_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ def test_form_with_nested_fields_for_inherit_validation_settings
assert_dom_equal expected, output_buffer
end

def test_form_with_nested_fields_inherit_validation_settings
form_with(model: @post, validate: true) do |f|
concat f.fields(:comment, model: @comment) { |c|
concat c.text_field(:title)
}
end

validators = { 'post[comment][title]' => { presence: [{ message: "can't be blank" }] } }
expected = whole_form_with('/posts', validators: validators) do
form_field('input', id: conditional_id('post_comment_title'), name: 'post[comment][title]', type: 'text')
end

assert_dom_equal expected, output_buffer
end

def test_form_with_nested_fields_for_inherit_validation_settings_when_record_object_is_a_hash
record_object = { defaults: nil }
post_with_category = Post.new
Expand Down
10 changes: 10 additions & 0 deletions test/action_view/cases/test_legacy_form_with_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ def test_form_with_fields_for
assert_dom_equal expected, result
end

def test_form_with_fields
result = fields(:comment, model: @comment) do |c|
c.text_field(:title)
end

expected = form_field('input', id: conditional_id('comment_title'), name: 'comment[title]', type: 'text')

assert_dom_equal expected, result
end

def test_form_with_select
form_with(model: @post) do |f|
concat f.select(:cost, [])
Expand Down

0 comments on commit cc318bc

Please sign in to comment.