Skip to content

Commit

Permalink
Fix to show validation errors in modals
Browse files Browse the repository at this point in the history
Fixes #1735
  • Loading branch information
mshibuya committed Oct 2, 2021
1 parent ac3fe35 commit f67defb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
8 changes: 4 additions & 4 deletions app/assets/javascripts/rails_admin/ra.remote-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@

form.bind("ajax:complete", function(event) {
var data = event.detail[0], status = event.detail[1];
if (status == 'error') {
dialog.find('.modal-body').html(data.responseText);
widget._bindFormEvents();
} else {
if (status == 'OK') {
var json = $.parseJSON(data.responseText);
var option = '<option value="' + json.id + '" selected>' + json.label + '</option>';
var select = widget.element.find('select').filter(":hidden");
Expand All @@ -116,6 +113,9 @@
}
widget._trigger("success");
dialog.modal("hide");
} else {
dialog.find('.modal-body').html(data.responseText);
widget._bindFormEvents();
}
});
},
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/rails_admin/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def handle_save_error(whereto = :new)

respond_to do |format|
format.html { render whereto, status: :not_acceptable }
format.js { render whereto, layout: false, status: :not_acceptable }
format.js { render whereto, layout: 'rails_admin/modal', status: :not_acceptable, content_type: Mime[:html].to_s }
end
end

Expand Down
5 changes: 5 additions & 0 deletions app/views/layouts/rails_admin/modal.js.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- flash && flash.each do |key, value|
.alert.alert-dismissible{class: flash_alert_class(key)}
%button.close{type: 'button', :'data-dismiss' => "alert"} &times;
= value
= yield
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/edit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Edit < RailsAdmin::Config::Actions::Base

respond_to do |format|
format.html { render @action.template_name }
format.js { render @action.template_name, layout: false }
format.js { render @action.template_name, layout: 'rails_admin/modal', content_type: Mime[:html].to_s }
end

elsif request.put? # UPDATE
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_admin/config/actions/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class New < RailsAdmin::Config::Actions::Base
end
respond_to do |format|
format.html { render @action.template_name }
format.js { render @action.template_name, layout: false }
format.js { render @action.template_name, layout: 'rails_admin/modal', content_type: Mime[:html].to_s }
end

elsif request.post? # CREATE
Expand Down
42 changes: 42 additions & 0 deletions spec/integration/widgets/remote_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,46 @@
expect(FieldTest.first.carrierwave_asset.file.size).to eq 1575
end
end

context 'with validation errors' do
before do
RailsAdmin.config Team do
field :players
end
RailsAdmin.config Player do
field :name
field :number
field :team
end
end

context 'on create' do
it 'shows the error messages' do
visit new_path(model_name: 'team')
click_link 'Add a new Player'
is_expected.to have_content 'New Player'
find('#player_name').set('on steroids')
find('#modal .save-action').click
is_expected.to have_css('#modal')
is_expected.to have_content 'Player is cheating'
is_expected.to have_css '.text-danger', text: 'is not a number'
end
end

context 'on update' do
let!(:player) { FactoryBot.create :player, name: 'Cheater' }

it 'shows the error messages' do
visit new_path(model_name: 'team')
find('option', text: 'Cheater').double_click
is_expected.to have_content "Edit Player 'Cheater'"
find('#player_name').set('Cheater on steroids')
find('#player_number').set('')
find('#modal .save-action').click
is_expected.to have_css('#modal')
is_expected.to have_content 'Player is cheating'
is_expected.to have_css '.text-danger', text: 'is not a number'
end
end
end
end

0 comments on commit f67defb

Please sign in to comment.