Home How to add validations in nested model form?
Reply: 0

How to add validations in nested model form?

user3782
1#
user3782 Published in July 23, 2018, 9:49 am

Suppose I am having this nested model form, as follows:-

<h1 class="text">Book Appointment</h1>


<%= form_for (@appointment) do |f| %>



  <div class="div-table">
    <div class="tr">
      <div class="tc">
        <div class="form-group">
          <%= f.label :Choose_Doctor %><br/>
          <%= f.collection_select(:doctor_id, @all_doctors, :id, :first_name ) %>
        </div>
      </div>
    </div>

    <div class="tr">
      <div class="tc">
        <div class="field">
          <%= f.label :Date %><br />
          <%= f.date_select :date, start_year: 2018, default: 1.days.from_now, id: 'selected_date' %><br>
          <%#= link_to "Check Availability", available_slots_appointments_path, id: "get_timeslots", remote: true %>
          <div id="error-box">
          </div>
        </div>
      </div>
    </div>


    <!-- <div class="tr">
      <div class="tc">
        <div class="field">
          <div id="available_slots">

          </div>
        </div>
      </div>
    </div> -->




    <div class="tr">
      <div class="tc">
        <div class="field">
          <h4>Cause:</h4>

            <%= f.fields_for :notes do |note_field| %>
                <%#= f.hidden_field :user_id, value: current_user.id %>
                <%= note_field.label :Note %><br>
                <%= note_field.text_area :description, placeholder: "Leave note here." %><br>
                <% if @appointment.errors.any? %>
                  <div id="error_explanation">
                      <% @appointment.errors.full_messages.each do |msg| %>
                        <div>
                          <%= msg %>
                        </div>
                      <% end %>
                  </div>
                <% end %>
            <% end %>

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

    <div class="tr">
      <div class="tc">
        <div class="field">
          <h4>Upload Disease Image</h4>

          <%= f.fields_for :images do |image_field| %>
            <%= image_field.label :Image %></br>
            <%= image_field.file_field :image %></br>
            <% if @appointment.errors.any? %>
              <div id="error_explanation">
                <% @appointment.errors.full_messages.each do |msg| %>
                <div>
                  <%= msg %>
                </div>
                <% end %>
              </div>
            <% end %>
          <% end %>
        </div>
      </div>
    </div>

    <div class = "tr">
      <div class="tc">
        <%= f.submit :book %>
      </div>
    </div>
  </div>

<% end %>

Suppose I have three models: Appointment, Image and Note. I have used accepts nested attributes for both image and note and used fields_for tag. After submitting the form, the image upload in getting vanished. Why? Secondly, how to add custom validations in nested model form?

EDIT: Answer is pretty simple. For nested attributes, validation still stays in the attribute model. Like Appointment has nested attribute Image. All the validations for Image will be put in Image Model. If you want to add validation which includes validating one model first and then second, you can add a custom validation using validates keyword

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316122 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO