-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from shivam091/0.2.3
0.2.3
- Loading branch information
Showing
4 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
|
||
label.is-invalid, .invalid-feedback { | ||
color: var(--bs-danger); | ||
} | ||
label.required::after { | ||
color: var(--bs-danger); | ||
content: "*"; | ||
padding-left: .25rem; | ||
top: -2px; | ||
font-weight: bolder; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- encoding: utf-8 -*- | ||
# -*- frozen_string_literal: true -*- | ||
# -*- warn_indent: true -*- | ||
|
||
module RailsBootstrapForm | ||
module Components | ||
module RequiredField | ||
extend ActiveSupport::Concern | ||
|
||
def self.included(base_class) | ||
def is_field_required?(attribute, options) | ||
return false unless attribute | ||
|
||
if options.key?(:required) | ||
options[:required] | ||
else | ||
is_attribute_required?(attribute) | ||
end | ||
end | ||
|
||
def required_field_options(options, attribute) | ||
required = is_field_required?(attribute, options) | ||
|
||
{}.tap do |option| | ||
option[:aria] = {required: true} if required | ||
option[:required] = required | ||
end | ||
end | ||
|
||
def is_attribute_required?(attribute) | ||
return false unless attribute | ||
|
||
target = object.instance_of?(Class) ? object : object.class | ||
return false unless target.respond_to?(:validators_on) | ||
|
||
has_presence_validator?(target_validators(target, attribute)) || | ||
is_required_association?(target, attribute) | ||
end | ||
|
||
def target_validators(target, attribute) | ||
target.validators_on(attribute).map(&:class) | ||
end | ||
|
||
def has_presence_validator?(target_validators) | ||
target_validators.include?(ActiveModel::Validations::PresenceValidator) || | ||
(defined?(ActiveRecord::Validations::PresenceValidator) && | ||
target_validators.include?(ActiveRecord::Validations::PresenceValidator)) | ||
end | ||
|
||
def is_required_association?(target, attribute) | ||
target.reflections.find do |name, a| | ||
next unless a.is_a?(ActiveRecord::Reflection::BelongsToReflection) | ||
next unless a.foreign_key == attribute.to_s | ||
|
||
has_presence_validator?(target_validators(target, name)) | ||
end | ||
end | ||
|
||
private :is_field_required?, :required_field_options, :target_validators, | ||
:has_presence_validator?, :is_required_association? | ||
end | ||
end | ||
end | ||
end |