【问题标题】:simple_form not detecting triggered validations for nested attributessimple_form 未检测到嵌套属性的触发验证
【发布时间】:2012-11-17 16:34:09
【问题描述】:

我有一个模型叫做“治疗”。它本质上是一个请求者和被请求者的会议提案,但也是一个 (1) :intro (2) :proposed_venue (3) :proposed_date (4) :proposed_time。

这四个属性(其中三个是嵌套属性)在表单上显示为字段。所有属性都有 validates_presence_of 验证,但只有 :intro 字段正在由我安装的 simple_form gem (https://github.com/plataformatec/simple_form) 进行验证。这一定是因为只有 :intro 属性(以上四个属性)是 Treating 模型的属性。 A Treating 有许多通过 Venue 模型提出的地点,以及许多通过 TDateTime 模型提出的日期和时间。

奇怪的是,如果所有四个属性都是空白的,我可以看到 Active Record 正在触发验证。只是 simple_form 并没有用“不能为空白”的通知将其空白字段突出显示为红色。

为了验证这一点,我在我的表单中添加了一个“@treating.errors.full_messages...”行,如果有四个空白字段,它会适当地返回四个错误(底部的屏幕截图):

<% if @treating.errors.any? %>

<ul>
  <% @treating.errors.full_messages.each do |msg| %>
  <li><%= msg %></li>
  <% end %>
</ul>

  <div class="modal-header">
    <h3><%= @user.first_name + " " + @user.last_name[0] + "." %></h3>
    <p>
      <% if @user.picture_url %>
        <%= image_tag(@user.picture_url, :size => "30x30") %>
      <% else %>
        <%= image_tag('smiley_small.png', :size => "30x30") %>
      <% end %> 
      <%= @user.headline %>
    </p>
  </div>

    <div class="modal-body">
      <div class="row-fluid">
        <form action="#" class="span12">
          <label for="treating-message"><h5>Introduce yourself to <%= @user.first_name %> (sample introductions):</h5></label>

          <%= f.hidden_field :requestee_id %>

          <%= f.label :intro, "Introduce yourself to " + @user.first_name + ":" %>
          <%= f.input :intro %>

          <%= f.simple_fields_for :t_date_times_attributes, :validate => { :presence => true } do |t_date_time| %>
            <%= t_date_time.simple_fields_for :"0" do |zero| %>  
              <%= zero.input :date, :input_html => { :value => params[:treating][:t_date_times_attributes][:'0'][:date] } %>
              <%= zero.input :time, :input_html => { :value => params[:treating][:t_date_times_attributes][:'0'][:time] } %>
            <% end %>
          <% end %>
        </div>

        <input class="bigdrop" id="e7" placeholder="Pick a venue with foursquare..." name="proposed_venue[foursquare_id]" />

        </form>
      </div>
    </div>

    <div class="modal-footer">
        <div class="field">
        <a href="#" class="btn btn-link" data-dismiss="modal">Close</a>
        <%= f.submit "Send", id: "send-button" %>
        </div>
    </div>

<% else %>

<ul>
<% @treating.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>

  <div id="modal-treating" class="modal hide fade">
  <div class="modal-header">
    <h3><%= @user.first_name + " " + @user.last_name[0] + "." %></h3>
    <p>
      <% if @user.picture_url %>
        <%= image_tag(@user.picture_url, :size => "30x30") %>
      <% else %>
        <%= image_tag('smiley_small.png', :size => "30x30") %>
      <% end %> 
      <%= @user.headline %>
    </p>
  </div>

  <div class="modal-body">
    <div class="row-fluid">
      <form action="#" class="span12">
        <label for="treating-message"><h5>Introduce yourself to <%= @user.first_name %> (sample introductions):</h5></label>

        <%= f.hidden_field :requestee_id %>

          <%= f.label :intro, "Introduce yourself to " + @user.first_name + ":" %>
          <%= f.input :intro %>

          <div class="control-group">
            <%= f.simple_fields_for :t_date_times_attributes, :validate => { :presence => true } do |t_date_time| %>
              <%= t_date_time.simple_fields_for :"0", :validate => { :presence => true } do |zero| %>
                <%= zero.input :date, :validate => { :presence => true } %>
                <%= zero.input :time, :validate => { :presence => true } %>
              <% end %>
            <% end %>
          </div>            

      <input class="bigdrop" id="e7" placeholder="Pick a venue with foursquare..." name="proposed_venue[foursquare_id]" />

      </form>
    </div>
  </div>

  <div class="modal-footer">
      <div class="field">
      <a href="#" class="btn btn-link" data-dismiss="modal">Close</a>
      <%= f.submit "Send", id: "send-button" %>
      </div>
  </div>

</div>

<% end %>

(根据页面顶部的错误消息,所有字段都应以红色突出显示,但只有 intro 字段[可能是因为它是唯一的非嵌套属性])。

虽然我认为这整个问题可能是 Ajax 的问题(请注意表单顶部的 remote true),但事实并非如此。重新加载新页面/提交空白字段时,我得到了相同的结果。

作为旁注,您还可以在图片中看到我的底部字段,提议的场地,在 Ajax 重新加载时失去了样式(整个表单实际上失去了所有样式)。这可能完全是另一个问题,但如果有人对为什么 css/js 会在 ajax 模态表单重新加载时被丢弃有任何想法,我很乐意知道。

我一直在反对这个问题太久了,我想我会向 SO 寻求帮助! 谢谢。

【问题讨论】:

    标签: ruby-on-rails forms validation nested-attributes


    【解决方案1】:

    如果你把这一切都做成一个表格,这会容易得多:

    <%= simple_form_for @treating do %>
      <%= fields_for :t_date_times do %>
        <% # etc... %>
    

    任何时候你都在编写自己的表单标签......你会很糟糕。这样 SimpleForm 可以计算出它应该从哪些对象中提取错误。不然真的没有办法知道。

    http://railscasts.com/episodes/196-nested-model-form-part-1 是典型的好例子 - 它与 SimpleForm 的工作原理相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-21
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      相关资源
      最近更新 更多