Uniqueness validation failing in a edit form with a nested model #431

Closed
elomarns opened this Issue Nov 7, 2012 · 3 comments

Comments

Projects
None yet
2 participants
@elomarns

elomarns commented Nov 7, 2012

I have a problem with uniqueness validation in a form with a nested model. The problem is on edit user page, where the user can add or remove companies associated with him. This is what happens:

1- The user visit the edit user page for the first time;
2- He add two or more companies and everything is ok;
3- He come back to the edit user page, and when the focus moves out from the first company input (name), Client Side Validation mark the input as invalid, but the user hasn't changed any data, he just gave focus to input, and moved out straight away.

The curious thing is that this doesn't happen on the last company inputs. So, if on the first visit to edit user page I added 3 companies, on the next visit, when name or website inputs of the 2 first companies loses focus, the problem occurs. But the inputs of the last company don't trigger the same error.

Here are the relevant models:

class User < ActiveRecord::Base
  # More code here

  has_many :companies, :foreign_key => :owner_id, :dependent => :destroy

  accepts_nested_attributes_for :companies, :reject_if => :all_blank, :allow_destroy => true
end
class Company < ActiveRecord::Base
  # More code here

  validates_uniqueness_of :name, :scope => :website, :case_sensitive => false,
    :allow_blank => true
  validates_uniqueness_of :website, :slug, :case_sensitive => false, :allow_blank => true

  belongs_to :owner, :class_name => "User"
end

Here is the edit user form:

<%= nested_form_for @user, :url => edit_user_registration_path, :validate => true,
  :html => { :method => :put, :id => :edit_user } do |f| -%>
    <!-- Fields working fine here --> 

    <div id="companies" class="content">
      <%= f.fields_for :companies do |company_form| %>
        <p>
          <%= f.text_field :name, :placeholder => "Company name (optional)" %>
        </p>

        <p>
          <%= f.text_field :website, :placeholder => "Company website (optional)" %>
        </p>

        <%= f.link_to_remove "Remove" %>
      <% end %>

      <p>
        <!-- I'm using Nested Form gem -->
        <%= f.link_to_add "Add a new company", :companies %>
      </p>
    </div>

    <!-- More working fields here -->

    <p>
      <%= f.submit "Edit" %>
    </p>
<% end -%>

Any idea on what I'm doing wrong?

@bcardarella

This comment has been minimized.

Show comment Hide comment
@bcardarella

bcardarella Nov 19, 2012

Contributor

I'll look into this. Thank you for the detailed bug report.

Contributor

bcardarella commented Nov 19, 2012

I'll look into this. Thank you for the detailed bug report.

@bcardarella

This comment has been minimized.

Show comment Hide comment
@bcardarella

bcardarella Nov 25, 2012

Contributor

Finally got the time to look into this, I notice that you're using nested_form. ClientSideValidations doesn't currently support this gem. However, we are currently working on a client project that will require it so keep an eye out for it.

Contributor

bcardarella commented Nov 25, 2012

Finally got the time to look into this, I notice that you're using nested_form. ClientSideValidations doesn't currently support this gem. However, we are currently working on a client project that will require it so keep an eye out for it.

@elomarns

This comment has been minimized.

Show comment Hide comment
@elomarns

elomarns Nov 26, 2012

Thank you!

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment