引导弹出窗口上的jQuery Validation插件错误消息

时间:2014-06-07 09:25:42

标签: jquery twitter-bootstrap jquery-validate

我正在尝试在bootstrap popover中显示验证错误消息,如此问题中所述:

How to use Twitter Bootstrap popovers for jQuery validation notifications?

但由于某些原因,我的代码无效。

这是我的代码:

的index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>

<html>
    <head>

        <!-- Website Title & Description for Search Engine purposes -->
        <title></title>
        <meta name="description" content="">

        <!-- Mobile viewport optimized -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

        <!-- Bootstrap CSS -->
        <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" >
        <link rel="stylesheet" type="text/css" href="includes/css/bootstrap-glyphicons.css" >


        <!-- Custom CSS -->
        <link href="includes/css/styles.css" rel="stylesheet">


        <!-- Include Modernizr in the head, before any other Javascript -->
        <script src="includes/js/modernizr-2.6.2.min.js"></script>

    </head>
    <body>
    ${greeting}


    <div class="container" id="main">

    <div class="navbar navbar-fixed-top">
    <div class="container">

        <!-- .btn-navbar is used as the toggle for collapsed navbar content -->
        <button class="navbar-toggle" data-target=".navbar-responsive-collapse" data-toggle="collapse" type="button">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>

        <a class="navbar-brand" href="/">ORG Info</a>

        <div class="nav-collapse collapse navbar-responsive-collapse">
            <ul class="nav navbar-nav">
                <li class="active">
                    <a href="home.html">Home</a>
                </li>

                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Services <strong class="caret"></strong></a>

                    <ul class="dropdown-menu">
                        <li>
                            <a href="#">Web Design</a>
                        </li>

                        <li>
                            <a href="#">Web Development</a>
                        </li>

                        <li>
                            <a href="#">SEO</a>
                        </li>

                        <li class="divider"></li>

                        <li class="dropdown-header">More Services</li>

                        <li>
                            <a href="#">Content Creation</a>
                        </li>

                        <li>
                            <a href="#">Social Media Marketing</a>
                        </li>
                    </ul><!-- end dropdown-menu -->
                </li>
            </ul>

            <form class="navbar-form pull-left">
                <input type="text" class="form-control" placeholder="Search this site..." id="searchInput">
                <button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button>
            </form><!-- end navbar-form -->

            <ul class="nav navbar-nav pull-right">
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-user"></span> My Account <strong class="caret"></strong></a>

                    <ul class="dropdown-menu">
                        <li>
                            <a href="#"><span class="glyphicon glyphicon-wrench"></span> Settings</a>
                        </li>

                        <li>
                            <a href="#"><span class="glyphicon glyphicon-refresh"></span> Update Profile</a>
                        </li>

                        <li>
                            <a href="#"><span class="glyphicon glyphicon-briefcase"></span> Billing</a>
                        </li>

                        <li class="divider"></li>

                        <li>
                            <a href="#"><span class="glyphicon glyphicon-off"></span> Sign out</a>
                        </li>
                    </ul>
                </li>
            </ul><!-- end nav pull-right -->
        </div><!-- end nav-collapse -->

    </div><!-- end container -->
</div><!-- end navbar -->


<!-- form start -->
<!--validation msg start  -->
<div class="popover right">
      <div class="arrow"></div>
      <h3 class="popover-title">Popover right</h3>
      <div class="popover-content">
        <p>Sed posuere consectetur est at lobortis. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum.</p>
      </div>
    </div>
<!--validation msg end  -->


<br><br><br><br>

<button type="button" id="examplebtn" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="right" data-content="Vivamus sagittis lacus vel augue laoreet rutrum faucibus.">
  Popover on left
</button>


<div class="row">
<div class="col-sm-6">
<form:form id="idCompanyReg" action="greeting.html" method="post" commandName="regform" enctype="multipart/form-data" class="form-horizontal" role="form" >
<fieldset>
<legend>Reg Form</legend>

<div class="form-group">
<form:label  path="name">Organization Name</form:label>
<form:input path="name" class="form-control input-medium required" placeholder="please enter name"  />
</div>
<!-- 
 <div class="form-group">
<label for="client_email">Email</label>
<div class="input-group">
<span class="input-group-addon"><i class="icon-envelope"></i></span>
<input id="client_email" name="client[email]" class="required email form-control" placeholder="Your Email" type="text">
</div>
</div>
-->



<div class="form-group">
<form:label path="type">Type(select one)</form:label>
<form:select class="form-control" path="type">
   <form:option class="form-control" value="">Select one</form:option>
   <form:option class="form-control" value="IT related company">IT related company</form:option>
   <form:option class="form-control" value="Hospital">Hospital</form:option>
   <form:option class="form-control" value="Educational Organization">Educational organizations</form:option>
   <form:option class="form-control" value="Other">Other</form:option>
</form:select> 
</div>

<div class="form-group">
<form:label path="place">Place:</form:label>
<form:input path="place" class="form-control"/>
</div>


<div class="form-group">
<form:label for="description" path="description">Description</form:label>
<form:textarea path="description" class="form-control"/>
</div>








<input type="file" name="file" title="Search for a profile pic to add..." class="btn btn-primary" >



<!-- <input type="file" id="file" name="file" /> -->

</fieldset>
 <hr/>
            <p><button type="submit" class="btn btn-primary">Submit</button></p>
</form:form >

</div>


<div class="col-sm-6"></div>
</div>
<!-- form end -->


    <div class="carousel slide" id="myCarousel">

    </div><!-- end myCarousel -->


    <div class="row" id="bigCallout">

    </div><!-- end row -->


    <div class="row" id="featuresHeading">

    </div><!-- end row -->


    <div class="row" id="features">

    </div><!-- end row -->


    <div class="row" id="moreInfo">

    </div><!-- end row -->


    <div class="row" id="moreCourses">

    </div><!-- end row -->

</div><!-- end container -->


<footer>

</footer>


    <!-- All Javascript at the bottom of the page for faster page loading -->

    <!-- First try for the online version of jQuery-->
    <script src="js/jquery-1.11.0.min.js"></script>

    <!-- If no online access, fallback to our hardcoded version of jQuery -->
    <script>window.jQuery || document.write('<script src="includes/js/jquery-1.8.2.min.js"><\/script>')</script>

    <!-- Bootstrap JS -->
    <script src="bootstrap/js/bootstrap.min.js"></script>

    <!-- Custom JS -->

    <script src="includes/js/bootstrap.file-input.js"></script>

    <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js"></script>
    <script src="includes/js/index.js"></script>


    </body>
</html>

index.js

$(document).ready(function(){
    $('#examplebtn').popover('show');


    $("#idCompanyReg").validate({
          rules: {
            name: {
              minlength: 3,
              required: true
            },
            place:{
              minlength: 3,
              required: true
            }
          },
          showErrors: function(errorMap, errorList) {
            $.each(this.successList, function(index, value) {
              return $(value).popover("hide");
            });
            return $.each(errorList, function(index, value) {
              var _popover;
              console.log(value.message);
              _popover = $(value.element).popover({
                trigger: "manual",
                placement: "top",
                content: value.message,
                template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-inner\"><div class=\"popover-content\"><p></p></div></div></div>"
              });
              _popover.data("popover").options.content = value.message;
              return $(value.element).popover("show");
            });
          }});



});

//document.ready end

错误消息:

TypeError: _popover.data(...) is undefined


_popover.data("popover").options.content = value.message;

JSFIDDLE

1 个答案:

答案 0 :(得分:1)

您的代码来自the jsFiddle ...

$("#idCompanyReg").validate({
        name: {
            required: true,
            minlength: 5,
            maxlength: 15
        },
        ...
    },

您的规则必须在rules选项的

$("#idCompanyReg").validate({
    rules: {  // <-- you need the rules option here
        name: {
            required: true,
            minlength: 5,
            maxlength: 15
        },
        ...
    },

至少jsFiddle现在正在运作:http://jsfiddle.net/3qYpM/420/