A
A
Anton Misyagin2015-05-31 23:58:11
Ruby on Rails
Anton Misyagin, 2015-05-31 23:58:11

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.
For an open tab, I would like to keep the javascript validation. The rest of the fields would be checked on the server. The fields are lit up. The user received a flash about the error and would go to other tabs, where he would find highlighted invalid fields. Is there such a possibility? Or is it possible to completely disable javascript validation for this particular form?
Another option is with several forms, where each form contains a group of fields. Then how to pass part of the validations on the server if only part of the data for the model came?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Misyagin, 2015-06-01
@sunnmas

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]

All these forms are submitted to the same route, which has not changed.
The action logic has changed:
Before:
def update
    bla(bla(bla))
    redirect_to edit_user_path
  end

It became:
def update
    bla(bla(bla))
    redirect_to edit_user_path(:tab=>params[:tab])
  end

For all fields of the model, I made valid default values. It turned out that the User record in the database was valid when it was created. Therefore, the validation logic in the model has not changed. How do you like this decision? How would you do?

D
darvetra, 2021-03-28
@darvetra

There was a similar problem with a hidden field in Chrome, I cured it by adding the readonly attribute to the field

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question