File tree Expand file tree Collapse file tree 3 files changed +32
-4
lines changed
lib/bootstrap_form/components Expand file tree Collapse file tree 3 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ class User < ApplicationRecord
1010 validates :email , presence : true , length : { minimum : 5 }
1111 validates :terms , acceptance : { accept : true }
1212
13+ # Conditional (always disabled) validators used in tests
14+ validates :status , presence : true , if : -> { age > 42 }
15+ validates :misc , presence : true , unless : -> { feet == 5 }
16+
1317 has_one :address
1418 accepts_nested_attributes_for :address
1519
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ def required_attribute?(obj, attribute)
2626 target = obj . instance_of? ( Class ) ? obj : obj . class
2727 return false unless target . respond_to? :validators_on
2828
29- presence_validator? ( target_validators ( target , attribute ) ) ||
29+ presence_validator? ( target_unconditional_validators ( target , attribute ) ) ||
3030 required_association? ( target , attribute )
3131 end
3232
@@ -35,12 +35,14 @@ def required_association?(target, attribute)
3535 next unless a . is_a? ( ActiveRecord ::Reflection ::BelongsToReflection )
3636 next unless a . foreign_key == attribute . to_s
3737
38- presence_validator? ( target_validators ( target , name ) )
38+ presence_validator? ( target_unconditional_validators ( target , name ) )
3939 end
4040 end
4141
42- def target_validators ( target , attribute )
43- target . validators_on ( attribute ) . map ( &:class )
42+ def target_unconditional_validators ( target , attribute )
43+ target . validators_on ( attribute )
44+ . reject { |validator | validator . options [ :if ] . present? || validator . options [ :unless ] . present? }
45+ . map ( &:class )
4446 end
4547
4648 def presence_validator? ( target_validators )
Original file line number Diff line number Diff line change @@ -644,4 +644,26 @@ class BootstrapCheckboxTest < ActionView::TestCase
644644 HTML
645645 assert_equivalent_html expected , @builder . check_box ( :email , label : "Email" )
646646 end
647+
648+ test "an attribute with required and if is not marked as required" do
649+ expected = <<~HTML
650+ < div class ="form-check mb-3 ">
651+ < input #{ autocomplete_attr } name="user[status]" type="hidden" value="0"/>
652+ < input class ="form-check-input " id ="user_status " name ="user[status] " type ="checkbox " value ="1 "/>
653+ < label class ="form-check-label " for ="user_status "> Status</ label >
654+ </ div>
655+ HTML
656+ assert_equivalent_html expected , @builder . check_box ( :status , label : "Status" )
657+ end
658+
659+ test "an attribute with presence validator and unless is not marked as required" do
660+ expected = <<~HTML
661+ < div class ="form-check mb-3 ">
662+ < input #{ autocomplete_attr } name="user[misc]" type="hidden" value="0"/>
663+ < input class ="form-check-input " id ="user_misc " name ="user[misc] " type ="checkbox " value ="1 "/>
664+ < label class ="form-check-label " for ="user_misc "> Misc</ label >
665+ </ div>
666+ HTML
667+ assert_equivalent_html expected , @builder . check_box ( :misc )
668+ end
647669end
You can’t perform that action at this time.
0 commit comments