简体   繁体   English

如何在客户端完全验证Rails form_for?

[英]How to validate Rails form_for is completely filled out on the client side?

I'm having a really hard time validating that all the form fields in my Rails form_for are completed before the form is allowed to be submitted to the backend. 我很难验证在允许将表单提交到后端之前,我的Rails form_for中的所有表单字段都已完成。 On the back the model checks for the presence of all the attributes, but on the front I need to alert the user if they haven't completed a field. 在背面,模型检查所有属性的存在,但是在正面,我需要警告用户是否尚未填写字段。 Here's the form: 形式如下:

<h1>List an item for sale</h1>
<%= form_for Product.new, :options => {:id => "new_listing"}, :url => {:action => "create"}, :html => {:multipart => true} do |f| %>
  <div><%= f.label :name %><br />
    <%= f.text_field :name, :id => "name_input", :autofocus => true, :placeholder => "Name yourself" %>
  </div>
  <div><%= f.label :price %><br />
    <%= f.number_field :decimal_price, :step => 'any', :placeholder => "Name your price" %>
  </div><br />
  <div><%= f.label :description %><br />
    <%= f.text_area :description, :cols => "50", :rows => "10", :placeholder => "Write a few sentences about the item you're listing. Is it in good condition? Are there any accessories included?" %>
  </div>
  <div>
    <%= f.label "Add a photo (increases your likelihood of selling)" %><br />
    <%= f.file_field :photo %>
  </div>
  <br />
  <div>
    <%= f.label "Category" %><br />
    <%= f.collection_select :category_id, Category.all, :id, :name, :prompt => 'Choose a category' %>
  </div><br />
  <%= f.submit "List!" %>
<% end %> 

The problem I've run into is figuring out how to send a false boolean value to the form so that it will stop the form from being sent. 我遇到的问题是弄清楚如何向表单发送错误的布尔值,以便它将阻止表单的发送。 Every time I've tried using onSubmit or onclick actions it just submits anyway and ignores the false boolean. 每当我尝试使用onSubmit或onclick动作时,它无论如何都会提交并忽略假布尔值。 Can someone show me how to check the form fields for name_input, price, and description to ensure they're completed and throw and error if they're not? 有人可以告诉我如何检查表单字段中的name_input,price和description来确保它们完成,如果没有则抛出错误。

If i understand you correctly... you should try to use 'event.preventDefault()' in your jQuery submit handler function: 如果我正确理解您的信息,则应在jQuery提交处理程序函数中尝试使用“ event.preventDefault()”:

 $( "#your-product-form-id" ).submit(function( event ) {
      if( your_validation_function() )
        return;
      else event.preventDefault();
    });

But for simple presence check I would add just html 'required' to input fields like this: 但是对于简单的状态检查,我只将html'required'添加到这样的输入字段中:

<%= f.text_field :name, :id => "name_input", :required => true %>

怎么样:required => true

<%= f.text_field :name, :id => "name_input", :autofocus => true, :placeholder => "Name yourself", required => true %>

You could use jQuery to bind a focusout or a keyup to each of the fields. 您可以使用jQuery将聚焦或绑定绑定到每个字段。 From there, you can make an AJAX request to your Rails controller to pull the values you wish to verify against. 从那里,您可以向Rails控制器发出AJAX请求,以提取您要验证的值。

Example: 例:

$("#name-input").focusout(function(){
  $.ajax({
   type: "GET",
   dataType "JSON",
   url: "insert_path_here"
   success: function(data){
     **INSERT VALIDATION CODE HERE***
   }

 }
});

Disabling the submit button in JavaScript until all of the required information has been entered would work. 在输入所有必需信息之前,禁用JavaScript中的“提交”按钮即可。 Just somehow indicate to the user that information is missing and once something has been entered enable the button. 只是以某种方式向用户指示信息丢失,一旦输入内容,就启用按钮。

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

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