Martijn Kerckhaert
Martijn Kerckhaert

Reputation: 494

Validation on AJAX edit/update

I have AJAX to create new items and update them in my view.

My model validation holds it from being saved or submitted when I create a new one.

However, when I update an existing field, it only stops saving it but it renders the ajax update form with the "new" data which gives the impression that it is being saved.

When I refresh afterwards I see the changes got rollbacked. But this is only half of the behaviour I want.

I need the submit button to be disabled. How can I achieve this?

My form is styled like this:

<%= f.text_field :name, required: true, class: "mdl-textfield__input" %>

and my validations in model look like this :

validates_presence_of :name

My edit.js.erb

$("#education_<%=@employee_education.id%>").replaceWith("<%= j render('new_employee_education_form')%>");

My update.js.erb

$("#edit_employeeeducation_<%= @employee_education.id %>").replaceWith('<%= j render partial: "employeeeducation", locals: {employee_education: @employee_education} %>');

enter image description here

Upvotes: 1

Views: 76

Answers (1)

Dawcars
Dawcars

Reputation: 368

Perhaps you could add some front end form validation using a jquery plugin eg formvalidator.net. You could use this prevent the form being submitted in the first place if the data is not correct.

Alternatively in your update.js.erb you should send some feedback to the user about why there is a problem with the data.

Upvotes: 1

Related Questions