这会影响我的验证,因为如果用户没有选择,那么仍然会使用""内。我的验证是收件人:[]。我可以取出"",但数组仍然存在,因此验证通过。
形式
<%= simple_form_for(mail, remote: true, format: :js) do |f| %>
<%= f.input :recipients, as: :select, input_html: { multiple: true, id: "recipients", include_hidden: false } %>
脚本
function templateDropDownFormat (item) {
return item.text;
}
function templateSelectionFormat (item) {
return item.text;
}
$("#recipients").select2({
placeholder: "To:",
multiple: true,
ajax: {
url: "/mails",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
params.page = params.page || 1;
return {
results: $.map(data, function(cnut) {
return {
text: cnut.name + ", " + cnut.city + ", " + cnut.country + " (" + cnut.account_type + ")",
id: cnut.account_type + "," + cnut.id,
};
}),
pagination: {
more: (params.page * 30) < data.total_count
}
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
templateResult: templateDropDownFormat, // omitted for brevity, see the source of this page
templateSelection: templateSelectionFormat// omitted for brevity, see the source of this page
});