2009-12-09 14:27:50 -05:00
|
|
|
module SimpleForm
|
2009-12-09 15:54:47 -05:00
|
|
|
module RequiredHelpers
|
2009-12-10 12:57:24 -05:00
|
|
|
# Attribute is always required, unless the user has defined the opposite.
|
2009-12-09 14:27:50 -05:00
|
|
|
def attribute_required?
|
2009-12-10 08:23:48 -05:00
|
|
|
options[:required] != false
|
2009-12-09 14:27:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def required_class
|
|
|
|
attribute_required? ? :required : :optional
|
|
|
|
end
|
|
|
|
|
2009-12-10 12:57:24 -05:00
|
|
|
# Creates default required classes for attributes, such as .string and
|
|
|
|
# .decimal, based on input type, and required class
|
2009-12-09 14:27:50 -05:00
|
|
|
def default_css_classes(merge_class=nil)
|
2009-12-10 08:23:48 -05:00
|
|
|
"#{input_type} #{required_class} #{merge_class}".strip
|
2009-12-09 14:27:50 -05:00
|
|
|
end
|
2009-12-10 09:11:29 -05:00
|
|
|
|
2009-12-10 12:57:24 -05:00
|
|
|
# When components may be required, default component html options always
|
|
|
|
# must include default css classes.
|
2009-12-10 09:11:29 -05:00
|
|
|
def component_html_options
|
|
|
|
html_options = super
|
|
|
|
html_options[:class] = default_css_classes(html_options[:class])
|
|
|
|
html_options
|
|
|
|
end
|
2009-12-09 14:27:50 -05:00
|
|
|
end
|
2009-12-10 12:57:24 -05:00
|
|
|
end
|