Skip to content

Commit

Permalink
Change: add novalidate attribute to the form tag when browser_validat…
Browse files Browse the repository at this point in the history
…ions are disabled
  • Loading branch information
dalpo committed Aug 4, 2015
1 parent b7d8c64 commit 163105d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/helpers/rails_admin/main_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
module RailsAdmin
module MainHelper
def rails_admin_form_for(*args, &block)
options = args.extract_options!.reverse_merge(builder: RailsAdmin::FormBuilder)
options = args.extract_options!.reverse_merge(
html: {novalidate: !RailsAdmin::Config.browser_validations},
builder: RailsAdmin::FormBuilder,
)

form_for(*(args << options), &block) << after_nested_form_callbacks
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class << self
# hide blank fields in show view if true
attr_accessor :compact_show_view

# Enable HTML5 browser validation
# Tell browsers whether to use the native HTML5 validations (novalidate form option).
attr_accessor :browser_validations

# Set the max width of columns in list view before a new set is created
Expand Down
39 changes: 39 additions & 0 deletions spec/helpers/rails_admin/main_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

describe RailsAdmin::MainHelper, type: :helper do
describe '#rails_admin_form_for' do
let(:html_form) do
helper.rails_admin_form_for(FieldTest.new, url: new_path(model_name: 'field_test')) {}
end

context 'with html5 browser_validations enabled' do
before do
RailsAdmin.config.browser_validations = true
RailsAdmin.config FieldTest do
field :address, :string do
required true
end
end
end

it 'should do something' do
expect(html_form).to_not include 'novalidate'
end
end

context 'with html5 browser_validations disabled' do
before do
RailsAdmin.config.browser_validations = false
RailsAdmin.config FieldTest do
field :address, :string do
required true
end
end
end

it 'should do something' do
expect(html_form).to include "novalidate=\"novalidate\""
end
end
end
end

0 comments on commit 163105d

Please sign in to comment.