Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果Select2字段为空,则LiveValidation提交表单而不验证其他字段_Javascript_Jquery_Html_Validation_Jquery Select2 - Fatal编程技术网

Javascript 如果Select2字段为空,则LiveValidation提交表单而不验证其他字段

Javascript 如果Select2字段为空,则LiveValidation提交表单而不验证其他字段,javascript,jquery,html,validation,jquery-select2,Javascript,Jquery,Html,Validation,Jquery Select2,我正在使用LiveValidation进行客户端表单验证 我还有一个多选表单字段,它应用了Select2 问题是,如果我将Select2字段留空而不选择任何内容,那么我的表单将在按下submit按钮后自动提交,而不会验证任何其他表单字段 但是,如果我确实在Select2字段中选择了一些内容并尝试提交表单,那么LiveValidation将尝试验证表单中的其他字段,包括Select2字段,如果验证失败,则表单不会提交,并提示用户首先修复错误 更新:即使我从我的multiselect表单字段中删除了

我正在使用LiveValidation进行客户端表单验证

我还有一个多选表单字段,它应用了Select2

问题是,如果我将Select2字段留空而不选择任何内容,那么我的表单将在按下submit按钮后自动提交,而不会验证任何其他表单字段

但是,如果我确实在Select2字段中选择了一些内容并尝试提交表单,那么LiveValidation将尝试验证表单中的其他字段,包括Select2字段,如果验证失败,则表单不会提交,并提示用户首先修复错误

更新:即使我从我的multiselect表单字段中删除了Select2,LiveValidation仍然会在保留为空/未选择任何内容时跳过验证

应用Select2的select字段我使用PHP函数getSelectOptions使用表中单个列的行值填充select的选项:

在这里,我通过添加Validate.Presence将LiveValidation应用于Select2之后的select字段。我告诉它select字段不能为空/必须有一个值:


我想加上这个作为一个评论,但我还没有代表。不过,我在RubyonRails中使用select2时也遇到了完全相同的问题:即,如果字段为空,那么在我的例子中也是一个multi-select,其他验证都会被跳过,我使用的是客户端验证和服务器端AJAX响应

但是,与您的情况一样,如果在select2字段中有一个选择,则所有其他验证都将正常启动

我糟糕的解决方法是在createaction服务器端选择空的select2字段,并通过AJAX将错误返回到表单所在的模式。这至少避免了我在控制台中遇到的令人讨厌的500错误。为了完整性,以及在ROR领域遇到此问题的任何其他人,黑客代码如下所示:

 def create
  # messaged is params definition of field contents
  if messaged.blank?
   #create a dummy message instance
    @message = Message.new
   #add error and call the whole thing off
    @message.errors.add(:base, "No recipients selected.")
    return
  end
  #rest of the normal action...
 end
以下是模态JS:

var el = $('#new-message-form');

<% if @message.errors.any? %>

  // Create a list of errors
  var errors = $('<ul />');

  <% @message.errors.full_messages.each do |error| %>
    errors.append('<li><%= escape_javascript( error ) %></li>');
  <% end %>

  // Display errors on form

  $('.modalerrors').html(errors).show('slideDown');
  $('.modalerrors').delay(5000).hide('slideUp');



<% else %>

    $('#messageModal').modal('hide');

    // hide then Clear form and reload page

    $('#messageModal').on('hidden', function () {
        el.find('input:text,textarea').val('');
        el.find('.modalerrors').empty();
       <% @message = "" %>
        location.reload();
    });
<% end %>

希望这对别人有帮助。或者更好的是,导致修复

我不知道你的意思。我的文本输入很好。它是当保留为空时无法正确验证的。
$(function()
{
...
var myselect = new LiveValidation("myselect", { onlyOnSubmit: true });          
myselect.add(Validate.Presence);
...
});
<input type="text" value="Your Name" name="name" onblur="if (this.value == '') {this.value        = 'Your Name';}"  onfocus="if (this.value == 'Your Name') {this.value = '';}" />
 if(!myselect) {
      throw error
  else { 
    //do what you need
   }
 def create
  # messaged is params definition of field contents
  if messaged.blank?
   #create a dummy message instance
    @message = Message.new
   #add error and call the whole thing off
    @message.errors.add(:base, "No recipients selected.")
    return
  end
  #rest of the normal action...
 end
var el = $('#new-message-form');

<% if @message.errors.any? %>

  // Create a list of errors
  var errors = $('<ul />');

  <% @message.errors.full_messages.each do |error| %>
    errors.append('<li><%= escape_javascript( error ) %></li>');
  <% end %>

  // Display errors on form

  $('.modalerrors').html(errors).show('slideDown');
  $('.modalerrors').delay(5000).hide('slideUp');



<% else %>

    $('#messageModal').modal('hide');

    // hide then Clear form and reload page

    $('#messageModal').on('hidden', function () {
        el.find('input:text,textarea').val('');
        el.find('.modalerrors').empty();
       <% @message = "" %>
        location.reload();
    });
<% end %>