Check with ParsleyJS and display errors with twitter tooltip

I would like to validate the form using parsley js and display errors (if any) using the twitter tooltip.

I read a link about integrating twitter bootstrap with parsley, and this question about the stack surface about EventListeners. However, I still cannot display error messages.

The way I implemented it

... 
<input id="id_email" name="email" required=True parsley-type="email">
<button type="submit" onclick="javascript:$('#id_email').parsley(parsleyOptions); ">Next</button>
...
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>   
<script type="text/javascript" src="{% static "js/bootstrap.min.js" %}"></script>
<script type="text/javascript" src="{% static "js/parsley/parsley.js"%}"></script>
...
    <script>

    // parsley option
    var parsleyOptions = {
      // Sets success and error class to Bootstrap class names
      successClass: 'has-success',
      errorClass: 'has-error',

      // Bootsrap needs success/error class to be set on parent element
      errors: {
        classHandler: function(el) {
          return el.parent();
        },
        // Set these to empty to make sure the default Parsley elements are not rendered
        errorsWrapper: '',
        errorElem: ''
      },
      listeners: {
        // Show a tooltip when a validation error occurs
        onFieldError: function (elem, constraints, parsleyField) {
          elem.tooltip({
            animation: false,
            container: 'body',
            placement: 'top',
            title: elem.data('error-message')
          });
        },
        // Hide validation tooltip if field is validated
        onFieldSuccess: function(elem, constraints, parsleyField) {
          elem.tooltip('destroy');
        }
      }
    };

    </script>
...

The tooltip does not start with an error in the form field (even with an empty field, the error does not occur). How can I get parsley to launch a tooltip?

Side notes:

  • The code for 'parsleyOptions' is taken from here
  • All js files available
  • static tooltip above input field works
  • parsley js , div
+3
1

, , , 2.x:

<form method="post" id="myForm">
    <input id="id_email" name="email" class="required" data-parsley-type="email">
    <button type="submit">Next</button>
</form>

<script type="text/javascript">
    $(document).ready(function() {  
        // instanciate parsley and set the container 
        // as the element title without a wrapper
        $("#myForm").parsley({
            errorsContainer: function (ParsleyField) {
                return ParsleyField.$element.attr("title");
            },
            errorsWrapper: false
        });

        // when there is an error, display the tooltip with the error message
        $.listen('parsley:field:error', function(fieldInstance) {
            var messages = ParsleyUI.getErrorsMessages(fieldInstance);
            fieldInstance.$element.tooltip('destroy');
            fieldInstance.$element.tooltip({
                animation: false,
                container: 'body',
                placement: 'top',
                title: messages
            });
        });

        // destroy tooltip when field is valid
        $.listen('parsley:field:success', function(fieldInstance) {     
            fieldInstance.$element.tooltip('destroy');
        });
    });
</script>

Bootstrap 3.2.0 Parsley.js 2.0.2. : http://jsfiddle.net/C96ab/3/

+4

All Articles