From d019cf929a5effaced66b2dfbdbfe4192478b04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serge=20H=C3=A4nni?= Date: Wed, 2 Feb 2022 21:48:37 +0100 Subject: [PATCH] Add custom validation contexts to `required` --- lib/simple_form/helpers/required.rb | 8 +++++++- lib/simple_form/helpers/validators.rb | 2 ++ test/inputs/required_test.rb | 28 +++++++++++++++++++++++++++ test/support/models.rb | 1 + 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/simple_form/helpers/required.rb b/lib/simple_form/helpers/required.rb index c3ab4cf21..3c94b973f 100644 --- a/lib/simple_form/helpers/required.rb +++ b/lib/simple_form/helpers/required.rb @@ -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? diff --git a/lib/simple_form/helpers/validators.rb b/lib/simple_form/helpers/validators.rb index 878746b03..a3dbd3000 100644 --- a/lib/simple_form/helpers/validators.rb +++ b/lib/simple_form/helpers/validators.rb @@ -34,6 +34,8 @@ def action_validator_match?(validator) !object.persisted? when :update object.persisted? + else + options[:context] == validator.options[:on] end end diff --git a/test/inputs/required_test.rb b/test/inputs/required_test.rb index 607ad1008..d6389abb9 100644 --- a/test/inputs/required_test.rb +++ b/test/inputs/required_test.rb @@ -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 diff --git a/test/support/models.rb b/test/support/models.rb index 17e0a53ce..155f8c33c 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -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