简体   繁体   English

.formValidation不是一个函数

[英].formValidation is not a function

Original Issue: 原始问题:

My form is hitting the validation correctly but submitting when it shouldn't be. 我的表单正确地验证了验证,但是在不应该的时候提交。

Following http://formvalidation.io/examples/ajax-submit/ 关注http://formvalidation.io/examples/ajax-submit/

Viewing my console I don't see any of the log statements in the .on "error" section or the "success" printed out. 查看我的控制台我没有看到.on “错误”部分中的任何日志语句或打印出的“成功”。 And the page just posts to itself. 页面只是发布到自己。 With the data in the url. 随着网址中的数据。 I don't know what I'm missing here as basically it skips all .on statements. 我不知道我在这里缺少什么,因为它基本上会跳过所有.on语句。

What is going on here? 这里发生了什么?

Remember Validation works just non of the .on statements. 请记住,验证仅适用于.on语句。

HTML: HTML:

<form id="service_path_form" class="form-horizontal">
<div class="form-group">
    <label class="col-xs-3 control-label">Service Name</label>
    <div class="col-xs-8">
        <input type="text" class="form-control" name="service_path_name" placeholder="Name" />
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">IP</label>
    <div class="col-xs-5">
        <input type="text" class="form-control" name="service_path_ip" placeholder="IP" />
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">Description</label>

    <div class="col-xs-8">
        <textarea class="form-control" name="service_path_description" rows="3" placeholder="Write a short Description"></textarea>
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">Enable</label>
    <div class="col-xs-5">
        <div class="radio">
            <label>
                <input type="radio" name="service_path_enabled" value="1" /> Enabled
            </label>
        </div>
        <div class="radio">
            <label>
                <input type="radio" name="service_path_enabled" value="0" /> Disabled
            </label>
        </div>
    </div>
</div>

<div class="form-group">
    <div class="col-xs-9 col-xs-offset-4">
        <button type="submit" class="btn btn-primary" name="validate" value="Validate and Submit">Validate and Submit</button>
    </div>
</div>
<input type="hidden" name="created_by" value="{{request.user}}">
<input type="hidden" name="date_created" id = "date_created" value="">

JS: JS:

<script>
$(document).ready(function() {
    $('#service_path_form')
      .bootstrapValidator({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            service_path_name: {
                // The messages for this field are shown as usual
                validators: {
                    notEmpty: {
                        message: 'The Service Path Name is required'
                    },
                }
            },
            service_path_ip: {
                // Show the message in a tooltip
                err: 'tooltip',
                validators: {
                    notEmpty: {
                        message: 'The destination ip address is required'
                    },
                    ip: {
                        message: 'The ip address is not valid'
                    }
                }
            },
            service_path_enabled: {
                // Show the message in a tooltip
                err: 'tooltip',
                validators: {
                    notEmpty: {
                        message: 'Do you want this service path to be actively monitored?'
                    }
                }
            }
        }
    })
    .on('err.form.fv', function(e) {
      e.preventDefault();
      console.log(e)
      console.log('test')
    })
    .on('success.form.fv', function(e) {
      // Prevent form submission
      console.log("MADE IT HERE")
      e.preventDefault();

      var $form = $(e.target),
          fv    = $form.data('formValidation');

      // Use Ajax to submit form data
      console.log("MADE IT HERE")
      $.ajax({
          url: "/servicepathapi/v1/servicepaths/",
          type: 'POST',
          data: $form.serialize(),
          success: function(result) {
              console.log(result)
          }
      });
  })
});
</script>

CURRENT: 当前:

Update with suggested upgrade: 建议升级更新:

I tried @Arkni 's suggestion, while I feel like it's in the right direction with I'm now getting this: 我试过@Arkni的建议,虽然我觉得它正朝着正确的方向发展,我现在得到了这个:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'app/scripts/js/bootstrap/bootstrap.min.js' %}" ></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="{% static 'form_validation/js/formValidation.js' %}" ></script>
<script src="{% static 'form_validation/js/framework/bootstrap.min.js' %}" ></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>


<script>
$(document).ready(function() {
    $('#service_path_form')
      .formValidation({
        framework: 'bootstrap',
        icon: {

With this output: 有了这个输出:

js控制台的屏幕截图

FIX: While checking all of my source files I noticed that I was actually loading jquery 2.0.2 and 2.1.3, removing 2.0.2 fixed the issue. FIX:在检查我的所有源文件时,我注意到我实际上正在加载jquery 2.0.2和2.1.3,删除2.0.2修复了问题。

As you said in your question, you are using FormValidation not BootstrapValidator . 正如您在问题中所说,您使用的是FormValidation,而不是BootstrapValidator

So to solve your problem, replace this 所以要解决你的问题,请更换它

$(document).ready(function() {
    $('#service_path_form')
        .bootstrapValidator({ // <====

with

$(document).ready(function() {
    $('#service_path_form')
        .formValidation({ // <====

Some notes: 一些说明:

Since you're using FormValidation, you should try to remove jquery.validate.min.js as I am afraid of the confliction. 由于您正在使用FormValidation,因此我应该尝试删除jquery.validate.min.js,因为我害怕冲突。

This jsfiddle works properly: 这个jsfiddle正常工作:

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/vendor/formvalidation/css/formValidation.min.css">

<form id="service_path_form" class="form-horizontal">
<div class="form-group">
    <label class="col-xs-3 control-label">Service Name</label>
    <div class="col-xs-8">
        <input type="text" class="form-control" name="service_path_name" placeholder="Name" />
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">IP</label>
    <div class="col-xs-5">
        <input type="text" class="form-control" name="service_path_ip" placeholder="IP" />
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">Description</label>

    <div class="col-xs-8">
        <textarea class="form-control" name="service_path_description" rows="3" placeholder="Write a short Description"></textarea>
    </div>
</div>

<div class="form-group">
    <label class="col-xs-3 control-label">Enable</label>
    <div class="col-xs-5">
        <div class="radio">
            <label>
                <input type="radio" name="service_path_enabled" value="1" /> Enabled
            </label>
        </div>
        <div class="radio">
            <label>
                <input type="radio" name="service_path_enabled" value="0" /> Disabled
            </label>
        </div>
    </div>
</div>

<div class="form-group">
    <div class="col-xs-9 col-xs-offset-4">
        <button type="submit" class="btn btn-primary" name="validate" value="Validate and Submit">Validate and Submit</button>
    </div>
</div>
</form>

The scripts: 脚本:

<script src="//code.jquery.com/jquery-2.1.3.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="/vendor/formvalidation/js/formValidation.min.js"></script>
<script src="/vendor/formvalidation/js/framework/bootstrap.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('#service_path_form')
  .formValidation({
    framework: 'bootstrap',
    icon: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        service_path_name: {
            // The messages for this field are shown as usual
            validators: {
                notEmpty: {
                    message: 'The Service Path Name is required'
                },
            }
        },
        service_path_ip: {
            // Show the message in a tooltip
            err: 'tooltip',
            validators: {
                notEmpty: {
                    message: 'The destination ip address is required'
                },
                ip: {
                    message: 'The ip address is not valid'
                }
            }
        },
        service_path_enabled: {
            // Show the message in a tooltip
            err: 'tooltip',
            validators: {
                notEmpty: {
                    message: 'Do you want this service path to be actively monitored?'
                }
            }
        }
    }
  });
});
</script>

Here is the result showing how it looks like: 以下结果显示了它的样子:

结果

I hope it helps you. 我希望它对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM