Skip to content

Commit 0d7fa83

Browse files
committed
Added FormBuilder#validate
Inject validators for attributes that are not rendered initially on the form object
1 parent d3fd32b commit 0d7fa83

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

lib/client_side_validations/action_view/form_builder.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ def client_side_form_settings(options, form_helper)
3434
end
3535
end
3636

37+
def validate(*attrs)
38+
attrs.each do |attr|
39+
build_validation_options(attr)
40+
end
41+
nil
42+
end
43+
3744
def initialize_with_client_side_validations(object_name, object, template, options, proc)
3845
initialize_without_client_side_validations(object_name, object, template, options, proc)
3946
@options[:validators] = { object => {} }

test/action_view/cases/test_helpers.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,13 @@ def test_text_field_with_custom_name
516516
assert_equal expected, output_buffer
517517
end
518518

519+
def test_text_field_with_added_validators
520+
form_for(@post, :validate => true) do |f|
521+
concat f.validate(:cost, :body, :title)
522+
end
523+
524+
validators = {'post[cost]' => {:presence => [{:message => "can't be blank"}]}, 'post[body]' => {:presence => [{:message => "can't be blank"}]}}
525+
expected = whole_form('/posts', 'new_post', 'new_post', :validators => validators)
526+
assert_equal expected, output_buffer
527+
end
519528
end

test/action_view/models/post.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Post
55
include ActiveModel::Conversion
66

77
attr_accessor :title, :author_name, :body, :secret, :written_on, :cost
8-
validates :cost, :presence => true
8+
validates :cost, :body, :presence => true
99

1010
def initialize(params={})
1111
params.each do |attr, value|

0 commit comments

Comments
 (0)