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
2 changes: 1 addition & 1 deletion lib/bootstrap_form/form_group_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def form_group_css_options(method, html_options, options)
end

def form_group_placeholder(options, method)
form_group_label_text(options[:label]) || object.class.human_attribute_name(method)
form_group_label_text(options[:label]) || (object && object.class.human_attribute_name(method)) || method.to_s.humanize # rubocop:disable Style/SafeNavigation
end
end
end
10 changes: 10 additions & 0 deletions test/bootstrap_form_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ class BootstrapFormGroupTest < ActionView::TestCase
assert_equivalent_html expected, @builder.text_field(:email, label_as_placeholder: true)
end

test "label as placeholder with form_with" do
expected = <<~HTML
<div class="mb-3">
<label class="visually-hidden required" for="user_email">Email</label>
<input required="required" class="form-control" id="user_email" placeholder="Email" name="user[email]" type="text" value="steve@example.com" />
</div>
HTML
assert_equivalent_html expected, form_with_builder.text_field(:email, label_as_placeholder: true, required: true)
end

test "adding prepend text" do
expected = <<~HTML
<div class="mb-3">
Expand Down
6 changes: 3 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def setup_test_fixture
end

# Originally only used in one test file but placed here in case it's needed in others in the future.
# This simulartes `form_with` without a model, which tests aspects that wouldn't otherwise
# be tested.
def form_with_builder
builder = nil
bootstrap_form_with(model: @user) { |f| builder = f }
builder
BootstrapForm::FormBuilder.new(:user, false, self, {})
end

def sort_attributes(doc)
Expand Down
Loading