当没有使用has_many检查复选框时显示验证错误:通过Rails 3 AJAX表单中的关联,使用client_side_validations gem

时间:2022-12-09 10:25:03

I'm getting great, inline error messages for all the other validations in this form, but no matter what I try, I can't seem to get any messages to show up after hitting submit for these checkboxes. The validation is working, as the form won't submit until I check a box, but I'd like to see some feedback for the user.

对于此表单中的所有其他验证,我收到了很好的内联错误消息,但无论我尝试什么,在点击提交这些复选框之后,我似乎无法显示任何消息。验证工作正常,因为在我选中一个框之前表单不会提交,但我希望看到一些反馈给用户。

My models (stripped down):

我的模特(剥离):

class Resource < ActiveRecord::Base
  attr_accessible :objective_ids

  has_many :obsources, :dependent => :destroy
  has_many :objectives, :through => :obsources

  validates_presence_of :objective_ids, :message => "Must check at least one"
end

class Objective < ActiveRecord::Base
  has_many :obsources, :dependent => :destroy
  has_many :resources, :through => :obsources
end

The form in my view (stripped down):

我视图中的表单(剥离):

<%= simple_form_for @resource, remote: true, :validate => true, :html => { :id => "resource-form#{@resource.skill_id}" } do |f| %>
  <%= f.hidden_field :skill_id, :value => @resource.skill_id %>
  <%= f.association :objectives, :label => "Objectives Targeted (at least 1)", :collection => Skill.find("#{@resource.skill_id}").objectives, :as => :check_boxes, :label_method => lambda { |objective| "#{objective.content}" } %>
  <%= f.submit %>
<% end %>

Thoughts would be much appreciated!

我们非常感谢您的想法!

2 个解决方案

#1


0  

And...I figured it out - sort of. I still can't get client_side_validations to throw me errors for my checkboxes, but I can get errors to show up when I try to submit the form by having my controller render the same create.js.erb, whether it could successfully save or not, then bring up the error messages in there, like this:

而且......我想通了 - 有点儿。我仍然无法获取client_side_validations来为我的复选框抛出错误,但是当我尝试通过让我的控制器呈现相同的create.js.erb来提交表单时,我可能会出现错误,是否可以成功保存,然后在那里调出错误信息,如下所示:

<% if @resource.errors.any? -%> 
  $('#objective-errors<%= @resource.skill_id %>').empty();
  $('#objective-errors<%= @resource.skill_id %>').prepend('<%= j render("errors") %>');
<% else %>
  // Whatever you want to happen on successful save
<% end %>

I also added an empty span with the objective-errors id right after the checkboxes, so that the errors show up inline, like all the other validation errors. This is bit messy, but happens to work because client_side_validations is making sure that everything else is validated before a save is even attempted, so the only errors ever returned after an unsuccessful save happen to be for my objectives.

我还在复选框后面添加了一个带有objective-errors id的空跨度,以便错误显示为内联,就像所有其他验证错误一样。这有点混乱,但碰巧工作,因为client_side_validations确保在甚至尝试保存之前确认其他所有内容都已经过验证,因此在不成功保存后发生的唯一错误恰好是我的目标。

So until I want to add another set of checkboxes, this works great! (Though if I ever do, it shouldn't be too difficult to filter the messages to show up next to the appropriate lists of checkboxes.)

所以在我想添加另一组复选框之前,这非常有用! (虽然如果我这样做,过滤消息以显示在相应的复选框列表旁边应该不会太难。)

#2


0  

For what it's worth (4.5 years later), I found a little hack for this. In my case, the checkbox was a disclaimer that the user had to accept before being able to proceed. Since checking and un-checking a checkbox doesn't actually change the value, and the client side validation library uses the input value, I just added the model-level validation and changed the input value in jQuery, which did the trick in terms of triggers the client side validation library.

为了它的价值(4。5年后),我发现了一点点黑客。在我的情况下,复选框是一个免责声明,用户必须先接受才能继续。由于检查和取消选中复选框并不会实际更改该值,并且客户端验证库使用输入值,我只是添加了模型级验证并更改了jQuery中的输入值,这样就可以实现触发客户端验证库。

In my model, I added:

在我的模型中,我补充说:

validates :field, inclusion: { in: [ true, 'true' ] }

Then, in JS on the view:

然后,在JS上的视图:

// For active toggling of checkbox
$('input#reference_finalization_referee_available').click( function() {
    if($(this).is(':checked')){
        $(this).val('true')
    } else {
        $(this).val('false')
    }
});

// On page load
if ($('input#reference_finalization_referee_available').is(':checked')) {
    $(this).val('true')
} else {
    $(this).val('false')
}

Kind of a hack, but does the trick.

一种黑客攻击,但诀窍。

#1


0  

And...I figured it out - sort of. I still can't get client_side_validations to throw me errors for my checkboxes, but I can get errors to show up when I try to submit the form by having my controller render the same create.js.erb, whether it could successfully save or not, then bring up the error messages in there, like this:

而且......我想通了 - 有点儿。我仍然无法获取client_side_validations来为我的复选框抛出错误,但是当我尝试通过让我的控制器呈现相同的create.js.erb来提交表单时,我可能会出现错误,是否可以成功保存,然后在那里调出错误信息,如下所示:

<% if @resource.errors.any? -%> 
  $('#objective-errors<%= @resource.skill_id %>').empty();
  $('#objective-errors<%= @resource.skill_id %>').prepend('<%= j render("errors") %>');
<% else %>
  // Whatever you want to happen on successful save
<% end %>

I also added an empty span with the objective-errors id right after the checkboxes, so that the errors show up inline, like all the other validation errors. This is bit messy, but happens to work because client_side_validations is making sure that everything else is validated before a save is even attempted, so the only errors ever returned after an unsuccessful save happen to be for my objectives.

我还在复选框后面添加了一个带有objective-errors id的空跨度,以便错误显示为内联,就像所有其他验证错误一样。这有点混乱,但碰巧工作,因为client_side_validations确保在甚至尝试保存之前确认其他所有内容都已经过验证,因此在不成功保存后发生的唯一错误恰好是我的目标。

So until I want to add another set of checkboxes, this works great! (Though if I ever do, it shouldn't be too difficult to filter the messages to show up next to the appropriate lists of checkboxes.)

所以在我想添加另一组复选框之前,这非常有用! (虽然如果我这样做,过滤消息以显示在相应的复选框列表旁边应该不会太难。)

#2


0  

For what it's worth (4.5 years later), I found a little hack for this. In my case, the checkbox was a disclaimer that the user had to accept before being able to proceed. Since checking and un-checking a checkbox doesn't actually change the value, and the client side validation library uses the input value, I just added the model-level validation and changed the input value in jQuery, which did the trick in terms of triggers the client side validation library.

为了它的价值(4。5年后),我发现了一点点黑客。在我的情况下,复选框是一个免责声明,用户必须先接受才能继续。由于检查和取消选中复选框并不会实际更改该值,并且客户端验证库使用输入值,我只是添加了模型级验证并更改了jQuery中的输入值,这样就可以实现触发客户端验证库。

In my model, I added:

在我的模型中,我补充说:

validates :field, inclusion: { in: [ true, 'true' ] }

Then, in JS on the view:

然后,在JS上的视图:

// For active toggling of checkbox
$('input#reference_finalization_referee_available').click( function() {
    if($(this).is(':checked')){
        $(this).val('true')
    } else {
        $(this).val('false')
    }
});

// On page load
if ($('input#reference_finalization_referee_available').is(':checked')) {
    $(this).val('true')
} else {
    $(this).val('false')
}

Kind of a hack, but does the trick.

一种黑客攻击,但诀窍。