简体   繁体   English

提交AJAX后如何验证表格?

[英]How to validate form after AJAX submit?

I have a form which I submit via AJAX so expected response format from server is javascript. 我有一个通过AJAX提交的表格,因此服务器的预期响应格式为javascript。 The problem is I can't make validation work like this. 问题是我无法像这样使验证工作。 Usually I would do it just like this: 通常我会这样做:

  # POST /cats
  # POST /cats.json
  def create
    @cat = Cat.new(cat_params)

    respond_to do |format|
      if @cat.save
        format.html { redirect_to @cat, notice: 'Cat was successfully created.' }
        format.json { render action: 'show', status: :created, location: @cat }
      else
        format.html { render action: 'new' }
        format.json { render json: @cat.errors, status: :unprocessable_entity }
      end
    end
  end 

But now I have a situation like this: 但是现在我有这样的情况:

  # POST /cats
  # POST /cats.json
  def create
    @cat = Cat.new(cat_params)

    respond_to do |format|
      if @cat.save
        format.js { }
      else
        format.js { ????? }
      end
    end
  end

I've created create.js.erb which does everything what's needed for me if new object is saved but I don't know how to process validation display if the object is not saved. 我创建了create.js.erb ,如果保存了新对象,它将执行我所需的一切,但是如果不保存对象,我不知道如何处理验证显示。 Inside my form I have this which works if I don't use AJAX: 在我的表单内部,如果不使用AJAX,则可以正常使用:

  <% if @cat.errors.any? %>
    <div id="error_explanation">
      <ul>
      <% @cat.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

What can I do? 我能做什么? I am using Rails 4. 我正在使用Rails 4。

In your create.js.erb you can render the form again unless @cat.persisted? unless @cat.persisted?unless @cat.persisted?您可以在create.js.erb再次呈现该表单unless @cat.persisted? .

  1. Take the form in another partial for example '_form.html.erb'. 以另一部分的形式表示,例如“ _form.html.erb”。

  2. If your form is inside a div with id "my_form_div", 如果您的表单位于ID为“ my_form_div”的div中,

then the create.js.erb will be 那么create.js.erb将是

<%  if @cat.persisted? %>
  # code for success save
<% else %>
  $('#my_form_div').html('<%= escape_javascript(render('form')) %>');
<% end %>

This will render the whole form again along with the errors. 这将再次呈现整个表格以及错误。 You can do the same for only the errors div if you don't want to render the full form again. 如果您不想再次呈现完整表单,则可以只对error div进行相同操作。

You can render create.js.erb in both cases success and failure and in you create.js.erb file check whether @cat.errors.any? 您可以在成功和失败两种情况下都渲染create.js.erb,并在create.js.erb文件中检查@ cat.errors.any是否? if so you can write some jquery and inject errors in the html if not so process with the success scenario as before. 如果是这样,您可以编写一些jquery并在html中注入错误,否则,请像以前一样处理成功方案。

sample code to illustrate what i mean ex: 示例代码来说明我的意思是:

def create
  @cat = Cat.new(cat_params)

  respond_to do |format|
   @error = @cat.save
   // or you can use if if you need more in success case to be done before rendering
   format.js {}
  end
end

in Your create.js.erb :: 在您的create.js.erb ::

<% if @error %>
  // loop over errors like your working code in the case of html request
<% else %>
  // success scenario
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM