我在rails 3中使用了nested_form,我有一个动态字段,用户可以添加或删除。
我需要知道那些字段的jquery选择器,所以我可以在字段之间进行一些javascript计算,目前动态字段将当前日期时间设置为字段,人们通常如何处理动态字段验证?
<%= nested_form_for [@project, @quote], :html => {:class =>'form-inline'} do |f| %>
<div>
<%= f.hidden_field :project_id %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :status %>
<%= f.text_field :status %>
</div>
<p>
<h4> Labour Items </h4>
</p>
<p>
<%= f.fields_for :labouritems, :html => { :class => 'form-vertical' } do |builder| %>
<%= builder.label :description %>
<%= builder.text_field :description, :class => "input-medium" %>
<%= builder.label :hours %>
<%= builder.text_field :hours, :class => "input-mini" %>
<%= builder.label :total %>
<%= builder.text_field :total, :class => "input-mini" %>
<%= builder.label "Mup/Hr" %>
<%= builder.text_field :muphourrate, :class => "input-mini" %>
<%= builder.label "Mup/Total" %>
<%= builder.text_field :muptotal, :class => "input-mini" %>
<%= builder.label "Mup/%" %>
<%= builder.text_field :muppercent, :class => "input-mini" %>
<%= builder.label "Contractor" %>
<%= builder.autocomplete_field :contractor_name, autocomplete_contractor_firstname_contractors_path, :update_elements => {:id => "##{form_tag_id(builder.object_name, :contractor_id)}" , :hourly_rate => "##{form_tag_id(builder.object_name, :contractor_rate)}" },:class => "input-small",:placeholder => "Seach" %>
<%= builder.hidden_field :contractor_id %>
<%= builder.label "Rate/hr" %>
<%= builder.text_field :contractor_rate,:class => "input-mini" %>
<%= builder.label "Work %" %>
<%= builder.text_field :progress_percent, :class => "input-mini" %>
<%= builder.label "Work $" %>
<%= builder.text_field :progress_payment, :class => "input-mini" %>
<%= builder.link_to_remove "Delete",:class => 'btn btn-small btn-danger' %>
<% end %>
<%= f.link_to_add "Add Item", :labouritems, :class => 'btn btn-small btn-primary' %>
</p>
答案 0 :(得分:0)
我不知道您需要验证哪种类型的信息,但这样的事情应该有所帮助。您要验证的所有字段都需要类或ID。如果您需要以某种方式验证某个问题,请为其指定一个ID并使用$("#elements_id")
来选择jQuery。
$(".form-inline").submit(function(){
$.each($(".input-mini"), function(){
if($(this).val() == ""){
$("#warnings").html("<p> Ooops, you forgot something </p>"); // Warn
return false; // Don't submit
}
}
});