开发者

How do I setup a error_message_on for a field that is part of a fields_for sub-form

开发者 https://www.devze.com 2023-01-13 20:46 出处:网络
I have a one to many relationship with a child form that is nested using a fields_for operation. I want to use the built in rails error handling to show error messages on the children using the erro

I have a one to many relationship with a child form that is nested using a fields_for operation.

I want to use the built in rails error handling to show error messages on the children using the error_message_on method.

Example:

      <% form_for @business, :url => {:action => :page, :page => @page}, :html => {:method => :put } do |f| -%>
        <h5><strong>More Details</strong></h5>
        <div class="clear"></div>
        <div class="col-1">
          <label for="hours">Hours</label>
          <table class="hours">
            <tbody>
              <% f.fields_for :hours do |hours_form| %>
              <tr>
                <td><%= hours_form.label :day, hours_form.object.day %>:<%= hours_form.hidden_field :day %&g开发者_运维技巧t;</td>
                <td><%= hours_form.text_field :open_time, :class => 'input' %></td>
                <td>to</td>
                <td><%= hours_form.text_field :close_time, :class => 'input' %></td>
                <td><%= hours_form.check_box :closed %></td>
                <td class="c6"><%= hours_form.label :closed, 'Closed this day' %>
                <%= hours_form.error_message_on :open_time, :css_class => 'cant-be-blank' %>
                <%= hours_form.error_message_on :close_time, :css_class => 'cant-be-blank' %></td>
              </tr>
              <% end -%>
            </tbody>
          </table>
...


Did u tried f.error_messages for displaying business errors and for child hours form, use hours_form.error_messages.

example

     <% form_for @business, :url => {:action => :page, :page => @page}, :html => {:method => :put } do |f| -%>
        <%= f.error_messages %>
        <h5><strong>More Details</strong></h5>
        <div class="clear"></div>
        <div class="col-1">
          <label for="hours">Hours</label>
          <table class="hours">
            <tbody>
              <% f.fields_for :hours do |hours_form| %>
              <%= hours_form.error_messages %>
              <tr>
                <td><%= hours_form.label :day, hours_form.object.day %>:<%= hours_form.hidden_field :day %></td>
                <td><%= hours_form.text_field :open_time, :class => 'input' %></td>
                <td>to</td>
                <td><%= hours_form.text_field :close_time, :class => 'input' %></td>
                <td><%= hours_form.check_box :closed %></td>
                <td class="c6"><%= hours_form.label :closed, 'Closed this day' %>
                <%= hours_form.error_message_on :open_time, :css_class => 'cant-be-blank' %>
                <%= hours_form.error_message_on :close_time, :css_class => 'cant-be-blank' %></td>
              </tr>
              <% end -%>
            </tbody>
          </table>
0

精彩评论

暂无评论...
验证码 换一张
取 消