Answer the question
In order to leave comments, you need to log in
How to disable javascript validation for part of simple_form form fields?
I am using the simple_form gem. The form was working. Data was entered into it from the database, it was possible to change the data and save it to the server. The data in simple_form goes through a two-step validation. First, javascript checks the validity of the data. Swears at various fields with input errors. If the browser considers the data correct, the form is sent to the server, where the model, when saved to the database, performs the same validations. If something is wrong - redirects to editing the form, where it highlights the fields containing errors. I wrote this as I understand the essence of the regular work of simple_form (correct if wrong).
Now to the problem. The form grew to gigantic proportions. Tabs are used to edit the form. Those. some of the fields are always hidden from the user. The user edits one tab, then moves to another, and so on. Then press send. The catch is that for hidden error fields, javascript throws an error
An invalid form control with name='user[field name]' is not focusable.The form is not submitted, no error is displayed. Here's how to get around the problem.
Answer the question
In order to leave comments, you need to log in
I decided to break the form into parts. It turned out 4 forms. At first I did not want to act in this way, because. I did not want the business logic of the controller to change for the sake of the interface. But everything turned out to be quite acceptable. The number of routes has not changed and the number of actions has not changed. This is good.
Old route:
New route:
View logic. Previously, it contained all fields available for editing, now, depending on the parameter, it renders the corresponding parsial:
%h1= @page_title = "Настройки профиля"
- if params[:tab] == "основные"
- @tab = :common
- elsif params[:tab] == "география"
- @tab = :geo
- elsif params[:tab] == "уведомления"
- @tab = :notify
- elsif params[:tab] == "интерфейс"
- @tab = :interface
- else
- @tab = :common
= render partial: "/cabinet/settings/tabs"
= simple_form_for current_user, :url => update_user_path, :html => {:multipart => true} do |f|
- if @tab == :common
= render partial: "/cabinet/settings/form_common", locals: {f: f}
- if @tab == :geo
= render partial: "/cabinet/settings/form_geo", locals: {f: f}
- if @tab == :notify
= render partial: "/cabinet/settings/form_notify", locals: {f: f}
- if @tab == :interface
= render partial: "/cabinet/settings/form_interface", locals: {f: f}
= f.button :submit, "Сохранить изменения"
= hidden_field_tag :tab, params[:tab]
def update
bla(bla(bla))
redirect_to edit_user_path
end
def update
bla(bla(bla))
redirect_to edit_user_path(:tab=>params[:tab])
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question