Skip to content
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
4 changes: 3 additions & 1 deletion lib/superform/rails/components/checkbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Superform
module Rails
module Components
class Checkbox < Field
prepend Concerns::Requirable

def view_template(&)
# Rails has a hidden and checkbox input to deal with sending back a value
# to the server regardless of if the input is checked or not.
Expand All @@ -16,4 +18,4 @@ def field_attributes
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/superform/rails/components/concerns/requirable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Superform
module Rails
module Components
module Concerns
module Requirable
def field_attributes
super.merge(validation_attributes)
end

def validation_attributes
return {} unless presence_validated?
{ required: true }
end

def presence_validated?
object = field.parent&.object
return false unless object&.class&.respond_to?(:validators_on)
object.class.validators_on(field.key).any? { |v| v.kind == :presence }
end
end
end
end
end
end
4 changes: 3 additions & 1 deletion lib/superform/rails/components/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Superform
module Rails
module Components
class Input < Field
prepend Concerns::Requirable

def view_template(&)
input(**attributes)
end
Expand Down Expand Up @@ -56,4 +58,4 @@ def attribute_type
end
end
end
end
end
4 changes: 3 additions & 1 deletion lib/superform/rails/components/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Superform
module Rails
module Components
class Select < Field
prepend Concerns::Requirable

def initialize(*, collection: [], **, &)
super(*, **, &)
@collection = collection
Expand Down Expand Up @@ -40,4 +42,4 @@ def map_options(collection)
end
end
end
end
end
4 changes: 3 additions & 1 deletion lib/superform/rails/components/textarea.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ module Superform
module Rails
module Components
class Textarea < Field
prepend Concerns::Requirable

def view_template(&content)
content ||= Proc.new { dom.value }
textarea(**attributes, &content)
end
end
end
end
end
end
76 changes: 75 additions & 1 deletion spec/superform/rails/field_convenience_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,78 @@
expect(component.type).to eq("radio")
end
end
end

describe "HTML5 client-side validations" do
context "input" do
it "adds required when presence validation exists" do
component = field.input
expect(component.field_attributes[:required]).to eq(true)
end

it "does not add required when no presence validation" do
component = form.field(:last_name).input
expect(component.field_attributes.key?(:required)).to eq(false)
end

it "allows required: false to override" do
component = field.input(required: false)
attrs = component.send(:attributes)
expect(attrs[:required]).to eq(false)
end
end

context "checkbox" do
it "adds required when presence validation exists" do
component = field.checkbox
expect(component.field_attributes[:required]).to eq(true)
end

it "does not add required when no presence validation" do
component = form.field(:last_name).checkbox
expect(component.field_attributes.key?(:required)).to eq(false)
end

it "allows required: false to override" do
component = field.checkbox(required: false)
attrs = component.send(:attributes)
expect(attrs[:required]).to eq(false)
end
end

context "textarea" do
it "adds required when presence validation exists" do
component = field.textarea
expect(component.field_attributes[:required]).to eq(true)
end

it "does not add required when no presence validation" do
component = form.field(:last_name).textarea
expect(component.field_attributes.key?(:required)).to eq(false)
end

it "allows required: false to override" do
component = field.textarea(required: false)
attrs = component.send(:attributes)
expect(attrs[:required]).to eq(false)
end
end

context "select" do
it "adds required when presence validation exists" do
component = field.select("a", "b")
expect(component.field_attributes[:required]).to eq(true)
end

it "does not add required when no presence validation" do
component = form.field(:last_name).select("a", "b")
expect(component.field_attributes.key?(:required)).to eq(false)
end

it "allows required: false to override" do
component = field.select("a", "b", required: false)
attrs = component.send(:attributes)
expect(attrs[:required]).to eq(false)
end
end
end
end