neurix
neurix

Reputation: 4316

Validate with ParsleyJS and display errors with twitter tooltip

I would like to validate a form with parsley js and display the errors (if any) with twitter's bootstrap tooltip.

I read this link about the twitter bootstrap integration with parsley and this stackoverflow question about EventListeners. However, I am still not able to display the error messages.

That's how 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 is not triggered with an error in the form field (even with an empty field, no error is raised). How can I get that parsley is triggering the tooltip to work?

Side notes:

Upvotes: 4

Views: 2505

Answers (1)

Lu&#237;s Cruz
Lu&#237;s Cruz

Reputation: 14980

For those who are interested in a way to accomplish this using parsley 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>

I'm using Bootstrap 3.2.0 and Parsley.js 2.0.2. I've created a fiddle with this: http://jsfiddle.net/C96ab/3/

Upvotes: 4

Related Questions