Skip to content

Commit 5b19352

Browse files
committed
Add a validation and fixer for modal submit buttons when inputs are present
1 parent 11bfbe1 commit 5b19352

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/block_kit/surfaces/modal.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ class Modal < Base
5050
fixes :close, truncate: {maximum: MAX_BUTTON_LENGTH}, null_value: {error_types: [:blank]}
5151

5252
validates :submit, presence: true, length: {maximum: MAX_BUTTON_LENGTH}, allow_nil: true
53+
validate :submit_present_if_contains_input
5354
fixes :submit, truncate: {maximum: MAX_BUTTON_LENGTH}, null_value: {error_types: [:blank]}
55+
fix :add_default_submit_button
5456

5557
def initialize(attributes = {})
5658
attributes = attributes.with_indifferent_access
@@ -69,6 +71,20 @@ def as_json(*)
6971
submit_disabled: submit_disabled
7072
).compact
7173
end
74+
75+
private
76+
77+
def submit_present_if_contains_input
78+
errors.add(:submit, "can't be blank when blocks contain input elements") if contains_input? && submit.blank?
79+
end
80+
81+
def add_default_submit_button
82+
self.submit = "Submit" if contains_input? && submit.blank?
83+
end
84+
85+
def contains_input?
86+
blocks.any? { |block| block.is_a?(Layout::Input) }
87+
end
7288
end
7389
end
7490
end

spec/block_kit/surfaces/modal_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@
255255
end
256256
end
257257

258-
context "validates" do
258+
context "validations" do
259259
it { is_expected.to validate_length_of(:private_metadata).is_at_most(3000).allow_nil }
260260
it { is_expected.to validate_length_of(:callback_id).is_at_most(255).allow_nil }
261261
it { is_expected.to validate_length_of(:external_id).is_at_most(255).allow_nil }
@@ -340,6 +340,22 @@
340340
expect(modal.errors["blocks[2].elements[0]"]).to include("is invalid: can't set focus_on_load when other elements have set focus_on_load")
341341
expect(modal.errors["blocks[2].elements[2]"]).to include("is invalid: can't set focus_on_load when other elements have set focus_on_load")
342342
end
343+
344+
it "validates that a submit button is present if there are inputs" do
345+
modal.header(text: "Header text")
346+
modal.section(text: "Info text")
347+
348+
expect(modal).to be_valid
349+
350+
modal.input(label: "Input label", element: BlockKit::Elements::PlainTextInput.new)
351+
352+
expect(modal).not_to be_valid
353+
expect(modal.errors[:submit]).to include("can't be blank when blocks contain input elements")
354+
355+
modal.submit = "Submit"
356+
expect(modal).to be_valid
357+
expect(modal.errors[:submit]).to be_empty
358+
end
343359
end
344360

345361
it "fixes individually-contained blocks" do
@@ -417,6 +433,20 @@
417433
}.from(true).to(false)
418434
end
419435

436+
it "fixes missing submit buttons when blocks contain inputs" do
437+
modal.header(text: "Header text")
438+
modal.section(text: "Info text")
439+
modal.input(label: "Input label", element: BlockKit::Elements::PlainTextInput.new)
440+
441+
expect {
442+
modal.fix_validation_errors
443+
}.to change {
444+
modal.submit
445+
}.from(nil).to(BlockKit::Composition::PlainText.new(text: "Submit"))
446+
447+
expect(modal).to be_valid
448+
end
449+
420450
it "can raise unfixed validation errors" do
421451
modal.header(text: "")
422452
modal.divider

0 commit comments

Comments
 (0)