Skip to content

Commit

Permalink
Remove validations on field :type option (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Mar 21, 2023
1 parent d6ce431 commit 3bddc69
Show file tree
Hide file tree
Showing 22 changed files with 33 additions and 40 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ CHANGELOG
---------

- **UNRELEASED**
- Nothing yet
- [View Diff](https://github.com/westonganger/form_builder.cr/compare/v1.0.1...master)
- [#4](https://github.com/westonganger/form_builder.cr/pulls/4) - Remove validations on field `:type` option

- **1.0.1** - Apr 24, 2021
- [View Diff](https://github.com/westonganger/form_builder.cr/compare/v1.0.0...v1.0.1)
- We now only support v0.36.1 and v1.0.0+
- ~~Update Crystal from v0.24.1 --> v0.34.0 (04/19/2020) --> v0.35.0 (06/09/2020)~~

- **1.0.0** - Feb 15, 2019
- Shard Initial Release
8 changes: 3 additions & 5 deletions spec/form_builder/builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ describe FormBuilder::Builder do
end

describe "#field" do
it "does not allow incorrect types" do
expect_raises(ArgumentError) do
builder.field(type: "submit", name: :foobar)
end
it "allows any arbitrary input type" do
builder.field(type: "submit", name: :foobar)
end

describe "input fields" do
INPUT_TYPES.each do |field_type|
(TESTED_FIELD_TYPES.to_a - NON_INPUT_TYPES.to_a).each do |field_type|

it "works for type: #{field_type}" do
expected = "<div><input type=\"#{field_type}\" foo=\"bar\" name=\"my-great-text-input\" id=\"my-great-text-input\"></div>"
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/bootstrap_2_horizontal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/bootstrap_2_inline_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/bootstrap_2_vertical_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/bootstrap_3_horizontal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/bootstrap_3_inline_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
6 changes: 3 additions & 3 deletions spec/form_builder/themes/bootstrap_3_vertical_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe theme_klass do
end

describe ".input_html_attributes" do
FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
it "returns correct #{field_type} attributes" do
attrs = StringHash.new

Expand All @@ -64,7 +64,7 @@ describe theme_klass do
end

describe ".label_html_attributes" do
FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
it "returns correct #{field_type} attributes" do
attrs = StringHash.new

Expand All @@ -81,7 +81,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/bootstrap_4_horizontal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/bootstrap_4_inline_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/bootstrap_4_vertical_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/bulma_horizontal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/bulma_vertical_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/default_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/foundation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/materialize_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/milligram_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/semantic_ui_inline_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes/semantic_ui_vertical_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe theme_klass do
end
end

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
describe ".input_html_attributes" do
it "returns correct #{field_type} attributes" do
attrs = StringHash.new
Expand Down
2 changes: 1 addition & 1 deletion spec/form_builder/themes_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe FormBuilder::Themes do
FormBuilder::Themes.classes.each do |theme_class|
b = FormBuilder::Builder.new(theme: theme_class.new)

FIELD_TYPES.each do |field_type|
TESTED_FIELD_TYPES.each do |field_type|
it "theme: #{theme_class.name}, field_type: #{field_type}" do
if field_type == "select"
actual = b.field type: :select, name: :foobar, label: "Hello", help_text: "World", errors: ["error1", "error2"], input_html: {class: "foo"}, label_html: {class: "foo"}, wrapper_html: {class: "foo"}, help_text_html: {class: "foo"}, error_html: {class: "foo"}, collection: {options: [["foo", "bar"], "foobar"], selected: "foobar", disabled: "other", include_blank: true}
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ require "../src/form_builder"

alias StringHash = Hash(String, String)

FIELD_TYPES = {"checkbox", "file", "hidden", "password", "radio", "select", "text", "textarea"}
INPUT_TYPES = FIELD_TYPES.to_a - ["select", "textarea"]
TESTED_FIELD_TYPES = {"checkbox", "file", "hidden", "password", "radio", "select", "text", "textarea", "date", "datetime-local", "time"}
NON_INPUT_TYPES = {"select", "textarea"}
15 changes: 4 additions & 11 deletions src/form_builder/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ module FormBuilder
class Builder
alias CollectionHash = Hash(String, (String | Symbol | Bool | Array(String) | Array(Array(String)) | Array(String | Array(String)) | Nil))

private FIELD_TYPES = {"checkbox", "file", "hidden", "password", "radio", "select", "text", "textarea"}
private INPUT_TYPES = {"checkbox", "file", "hidden", "password", "radio", "text"}
private NON_INPUT_TYPES = {"select", "textarea"}
private COLLECTION_KEYS = {"options", "selected", "disabled", "include_blank"}

@theme : FormBuilder::Themes::BaseTheme
Expand Down Expand Up @@ -46,10 +45,6 @@ module FormBuilder
)
type_str = type.to_s

unless FIELD_TYPES.includes?(type_str)
raise ArgumentError.new("Invalid :type argument, valid field types are: #{FIELD_TYPES.join(", ")}`")
end

if collection && type_str != "select"
raise ArgumentError.new("Argument :collection is not supported for type: :#{type_str}")
end
Expand Down Expand Up @@ -84,7 +79,7 @@ module FormBuilder
end
end

if !themed_input_html["value"]? && value && !value.to_s.empty? && INPUT_TYPES.includes?(type_str)
if !themed_input_html["value"]? && value && !value.to_s.empty? && !NON_INPUT_TYPES.includes?(type_str)
themed_input_html["value"] = value
end

Expand Down Expand Up @@ -191,6 +186,8 @@ module FormBuilder
s << themed_input_html["value"]?
s << "</textarea>"
end
else
html_field = input_field(type: type_str, attrs: themed_input_html)
end

if label != false
Expand Down Expand Up @@ -224,10 +221,6 @@ module FormBuilder
end

private def input_field(type : String, attrs : StringHash? = StringHash.new)
unless INPUT_TYPES.includes?(type.to_s)
raise ArgumentError.new("Invalid input :type, valid input types are `#{INPUT_TYPES.join(", ")}`")
end

attrs.delete("type")

boolean_opts = {"disabled"}
Expand Down

0 comments on commit 3bddc69

Please sign in to comment.