验证表单中嵌套属性的存在 [英] Validate presence of nested attributes within a form
问题描述
我有以下关联:
#models/contact.rb
class Contact < ActiveRecord::Base
has_many :contacts_teams
has_many :teams, through: :contacts
accepts_nested_attributes_for :contacts_teams, allow_destroy: true
end
#models/contacts_team.rb
class ContactsTeam < ActiveRecord::Base
belongs_to :contact
belongs_to :team
end
#models/team.rb
class Team < ActiveRecord::Base
has_many :contacts_team
has_many :contacts, through: :contacts_teams
end
一个contact
应该始终至少具有一个关联的团队(在contacts_teams
的富连接表中指定).
A contact
should always have at least one associated team (which is specified in the rich join table of contacts_teams
).
如果用户尝试在没有关联团队的情况下创建联系人,则应进行验证.如果用户尝试删除联系人的所有关联团队,则应进行验证.
If the user tried to create a contact without an associated team: a validation should be thrown. If the user tries to remove all of a contact's associated teams: a validation should be thrown.
我该怎么做?
I did look at the nested attributes docs. I also looked at this article and this article which are both a bit dated.
完成操作:我正在使用nested_form_fields
gem向联系人动态添加新的关联团队.这是表格中的相关部分(有效,但目前尚未验证至少一个团队与该联系人相关联):
For completion: I am using the nested_form_fields
gem to dynamically add new associated teams to a contact. Here is the relevant part on the form (which works, but currently not validating that at least one team was associated to the contact):
<%= f.nested_fields_for :contacts_teams do |ff| %>
<%= ff.remove_nested_fields_link %>
<%= ff.label :team_id %>
<%= ff.collection_select(:team_id, Team.all, :id, :name) %>
<% end %>
<br>
<div><%= f.add_nested_fields_link :contacts_teams, "Add Team"%></div>
因此,如果未单击添加团队",则不会通过与团队相关的参数,因此不会创建任何contacts_team
记录.但是,当单击添加团队"并选择了一个团队并提交了表单时,将通过以下参数传递类似这样的信息:
So when "Add Team" is not clicked then nothing gets passed through the params related to teams, so no contacts_team
record gets created. But when "Add Team" is clicked and a team is selected and form submitted, something like this gets passed through the params:
"contacts_teams_attributes"=>{"0"=>{"team_id"=>"1"}}
推荐答案
这将进行创建和更新contact
的验证:确保至少有一个关联的contacts_team
.当前存在一种边缘情况,这会导致较差的用户体验. 我在此处发布了该问题.尽管在大多数情况下,这可以解决问题.
This does the validations for both creating and updating a contact
: making sure there is at least one associated contacts_team
. There is a current edge case which leads to a poor user experience. I posted that question here. For the most part though this does the trick.
#custom validation within models/contact.rb
class Contact < ActiveRecord::Base
...
validate :at_least_one_contacts_team
private
def at_least_one_contacts_team
# when creating a new contact: making sure at least one team exists
return errors.add :base, "Must have at least one Team" unless contacts_teams.length > 0
# when updating an existing contact: Making sure that at least one team would exist
return errors.add :base, "Must have at least one Team" if contacts_teams.reject{|contacts_team| contacts_team._destroy == true}.empty?
end
end
这篇关于验证表单中嵌套属性的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!