Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom validation contexts in required #1759

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/simple_form/helpers/required.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ def calculate_required
end

def required_by_validators?
(attribute_validators + reflection_validators).any? { |v| v.kind == :presence && valid_validator?(v) }
(attribute_validators + reflection_validators).any? { |v| v.kind == :presence && valid_validator?(v) && required_by_validation_context?(v) }
end

def required_by_validation_context?(validator)
return true if options[:context].blank? || validator.options[:on].blank?

validator.options[:on] == options[:context]
end

def required_by_default?
Expand Down
2 changes: 2 additions & 0 deletions lib/simple_form/helpers/validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def action_validator_match?(validator)
!object.persisted?
when :update
object.persisted?
else
options[:context] == validator.options[:on]
end
end

Expand Down
28 changes: 28 additions & 0 deletions test/inputs/required_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,34 @@ class RequiredTest < ActionView::TestCase
assert_select 'input.optional#validating_user_phone_number'
end

test 'builder input does not be required when validation is on custom context which is not active' do
with_form_for @validating_user, :home_picture
assert_no_select 'input.required'
assert_no_select 'input[required]'
assert_select 'input.optional#validating_user_home_picture'
end

test 'builder input is required when validation is on custom context which is active' do
with_form_for @validating_user, :home_picture, context: :with_picture
assert_select 'input.required'
assert_select 'input[required]'
assert_select 'input.required[required]#validating_user_home_picture'
end

test 'builder input is required when validation has no custom context, but another one is active' do
with_form_for @validating_user, :name, context: :with_picture
assert_select 'input.required'
assert_select 'input[required]'
assert_select 'input.required[required]#validating_user_name'
end

test 'builder input does not be required when validation has no custom context, but another one is active' do
with_form_for @validating_user, :attempts, context: :with_picture
assert_no_select 'input.required'
assert_no_select 'input[required]'
assert_select 'input.optional#validating_user_attempts'
end

test 'builder input does not generate required html attribute when option is set to false when it is set to true in wrapper' do
swap SimpleForm, browser_validations: true do
swap_wrapper :default, self.custom_wrapper_with_required_input do
Expand Down
1 change: 1 addition & 0 deletions test/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class ValidatingUser < User
validates :company, presence: true
validates :age, presence: true, if: proc { |user| user.name }
validates :amount, presence: true, unless: proc { |user| user.age }
validates :home_picture, presence: true, on: :with_picture

validates :action, presence: true, on: :create
validates :credit_limit, presence: true, on: :save
Expand Down