RamboBambo
RamboBambo

Reputation: 86

Form init and field init events do not work in parsley.js

Here's a simple example: http://codepen.io/spacejaguar/pen/KrvqNW

html:

<form data-parsley-validate>
  <label for="name">Name:</label>
  <input type="text" name="name" required>
  <br>
  <input type="submit" value="validate">

  <p><small>This is a simplistic example, good to start from when giving examples of use of Parsley</small></p>
</form>

and JS

$(function () {
    $('form').parsley()
    .on('form:init', function() {
        console.log('Form init', arguments);
    })
    .on('field:init', function() {
        console.log('Field init', arguments);
    })
    .on('form:validate', function() {
        console.log('Form validate', arguments);
    })
    .on('form:submit', function() {
        return false; // Don't submit form for this demo
    });
});

It seems that form:init or field:init callback functions are not called at all, while any other one works just fine. What do i do wrong? Or maybe it's a bug?

[EDIT]
I looked into source code and did some debugging stuff, it seems that init event is triggered before any listener has been attached. Creating parsley instance looks alike:

Upvotes: 4

Views: 1471

Answers (1)

Jonathan
Jonathan

Reputation: 1695

I had the same problem and thanks to your research on the event triggering I found a solution. Just bind the event handler to the window.Parsley object and it will work just fine.

For example:

window.Parsley.on('form:init', () => console.log('form:init'))

Upvotes: 2

Related Questions