Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add required option on form_group_builder #488

Merged
merged 7 commits into from
Nov 7, 2018
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [461](https://github.com/bootstrap-ruby/bootstrap_form/pull/461): default form-inline class applied to parent content div on date select helpers. Can override with a :skip_inline option on the field helper - [@lancecarlson](https://github.com/lancecarlson).
* Your contribution here!
* The `button`, `submit`, and `primary` helpers can now receive an additional option, `extra_class`. This option allows us to specify additional CSS classes to be added to the corresponding button/input, _while_ maintaining the original default ones. E.g., a primary button with an `extra_class` 'test-button' will have its final CSS classes declaration as 'btn btn-primary test-button'.
* [#488](https://github.com/bootstrap-ruby/bootstrap_form/pull/488): add required option on form_group_builder - [@ThomasSevestre](https://github.com/ThomasSevestre).

### Bugfixes

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,11 @@ validator with the associated model attribute. Presently this is one of:
ActiveRecord::Validations::PresenceValidator or
ActiveModel::Validations::PresenceValidator.

In cases where this behavior is undesirable, use the `skip_required` option:
In cases where this behavior is undesirable, use the `required` option to force the class to be present or absent:

```erb
<%= f.password_field :password, label: "New Password", skip_required: true %>
<%= f.password_field :login, label: "New Username", required: true %>
<%= f.password_field :password, label: "New Password", required: false %>
```

### Input Elements / Controls
Expand Down
12 changes: 10 additions & 2 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,15 @@ def form_group_builder(method, options, html_options = nil)
label_text ||= options.delete(:label)
end

if options.key?(:skip_required)
warn "`:skip_required` is deprecated, use `:required: false` instead"
options[:required] = options.delete(:skip_required) ? false : :default
end

form_group_options[:label] = {
text: label_text,
class: label_class,
skip_required: options.delete(:skip_required)
required: options.delete(:required)
}.merge(css_options[:id].present? ? { for: css_options[:id] } : {})

if options.delete(:label_as_placeholder)
Expand Down Expand Up @@ -463,7 +468,10 @@ def generate_label(id, name, options, custom_label_col, group_layout)
classes << "mr-sm-2"
end

unless options.delete(:skip_required)
case options.delete(:required)
when true
classes << "required"
when nil, :default
classes << "required" if required_attribute?(object, name)
end

Expand Down
22 changes: 21 additions & 1 deletion test/bootstrap_form_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BootstrapFormGroupTest < ActionView::TestCase
assert_equivalent_xml expected, @builder.text_field(:email, skip_label: true)
end

test "preventing a label from having the required class" do
test "preventing a label from having the required class with :skip_required" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label for="user_email">Email</label>
Expand All @@ -84,6 +84,26 @@ class BootstrapFormGroupTest < ActionView::TestCase
assert_equivalent_xml expected, @builder.text_field(:email, skip_required: true)
end

test "preventing a label from having the required class" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" type="text" value="steve@example.com" />
</div>
HTML
assert_equivalent_xml expected, @builder.text_field(:email, required: false)
end

test "forcing a label to have the required class" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
<label class="required" for="user_comments">Comments</label>
<input class="form-control" id="user_comments" name="user[comments]" type="text" value="my comment" />
</div>
HTML
assert_equivalent_xml expected, @builder.text_field(:comments, required: true)
end

test "label as placeholder" do
expected = <<-HTML.strip_heredoc
<div class="form-group">
Expand Down