Skip to content

Commit

Permalink
Merge bb21e40 into 4645082
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed Apr 19, 2020
2 parents 4645082 + bb21e40 commit ec4f691
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## master / unreleased

* [FEATURE] Drop Ruby 2.3 support
* [FEATURE] Add support for many association validations ([#783](https://github.com/DavyJonesLocker/client_side_validations/pull/783))

## 16.2.0 / 2020-04-10

Expand Down
9 changes: 6 additions & 3 deletions lib/client_side_validations/action_view/form_helper.rb
Expand Up @@ -99,12 +99,15 @@ def add_validator(validator_hash, validation_hash, name, attr)
if validation_hash.key?(attr)
validator_hash[name] = validation_hash[attr]
elsif attr.to_s.ends_with?('_id')
add_validator_with_association validator_hash, validation_hash, name, attr
association_name = attr.to_s.gsub(/_id\Z/, '').to_sym
add_validator_with_association validator_hash, validation_hash, name, association_name
elsif attr.to_s.ends_with?('_ids')
association_name = attr.to_s.gsub(/_ids\Z/, '').pluralize.to_sym
add_validator_with_association validator_hash, validation_hash, name, association_name
end
end

def add_validator_with_association(validator_hash, validation_hash, name, attr)
association_name = attr.to_s.gsub(/_id\Z/, '').to_sym
def add_validator_with_association(validator_hash, validation_hash, name, association_name)
return unless validation_hash.key?(association_name)

validator_hash[name] = validation_hash[association_name]
Expand Down
24 changes: 24 additions & 0 deletions test/action_view/cases/test_form_for_helpers.rb
Expand Up @@ -383,6 +383,30 @@ def test_collection_select_with_association
assert_dom_equal expected, output_buffer
end

def test_collection_check_boxes_with_many_association
form_for(@post, validate: true) do |f|
concat f.collection_check_boxes(:tag_ids, [], :id, :title)
end

validators = {
'post[tag_ids]' => {
length: [{
messages: {
minimum: 'is too short (minimum is 0 characters)',
maximum: 'is too long (maximum is 3 characters)'
},
minimum: 0,
maximum: 3
}]
}
}

expected = whole_form_for('/posts', 'new_post', 'new_post', validators: validators) do
form_field('input', name: 'post[tag_ids][]', type: 'hidden', value: '')
end
assert_dom_equal expected, output_buffer
end

def test_collection_select_with_validate_options
form_for(@post, validate: true) do |f|
concat f.collection_select(:cost, [], :id, :name, {}, validate: false)
Expand Down
4 changes: 4 additions & 0 deletions test/action_view/models/post.rb
Expand Up @@ -9,6 +9,7 @@ class Post
attr_accessor :title, :author_name, :body, :secret, :written_on, :cost
validates :cost, :body, presence: true
validates :body, length: { minimum: 200 }
validates :tags, length: { minimum: 0, maximum: 3 }

# Simulate default Rails 5's association
validates :category, presence: { message: :required }
Expand All @@ -28,4 +29,7 @@ def comments_attributes=(attributes); end

attr_accessor :category, :category_id
def category_attributes=(attributes); end

attr_accessor :tags, :tag_ids
def tags_attributes=(attributes); end
end
20 changes: 20 additions & 0 deletions test/action_view/models/tag.rb
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class Tag
extend ActiveModel::Naming
extend ActiveModel::Translation
include ActiveModel::Validations
include ActiveModel::Conversion

attr_reader :id, :title, :description

def initialize(params = {})
params.each do |attr, value|
public_send("#{attr}=", value)
end
end

def persisted?
false
end
end

0 comments on commit ec4f691

Please sign in to comment.