Getting Parsley 2.x working with Bootstrap 3 correctly
Asked Answered
K

2

18

I am using Twitter Bootstrap 3.1.1 with Parsley v2.0.0-rc3. I made it mostly work apart from classHandler option.

I have HTML like this:

<div class="form-group">
    <label class="control-label" for="username">User Name</label>
    <input class="form-control" id="username" name="username" required="" type="text" value="">   
</div>

And run Parsley like this:

$("#register_form").parsley({
    successClass: "has-success",
    errorClass: "has-error",
    classHandler: function(el) {
        return $(el).closest(".form-group");
    },
    errorsWrapper: "<span class='help-block'></span>",
    errorTemplate: "<span></span>"
});

Everything works fine, but success / error classes are applied to wrong element. After the page is loaded, I get this:

<div class="form-group">
    <label class="control-label" for="username">User Name</label>
    <input class="form-control" id="username" name="username" required="" type="text" value="" data-parsley-id="5043">
    <span class="help-block" id="parsley-id-5043"></span>
</div>

And when validated, the result is this:

<div class="form-group">
    <label class="control-label" for="username">User Name</label>
    <input class="form-control has-error" id="username" name="username" required="" type="text" value="" data-parsley-id="5043">
    <span class="help-block filled" id="parsley-id-5043">
        <span class="parsley-required">This value is required.</span>
    </span>
</div>

However, I expect something different:

<div class="form-group has-error">
    <label class="control-label" for="username">User Name</label>
    <input class="form-control" id="username" name="username" required="" type="text" value="" data-parsley-id="5043">
    <span class="help-block filled" id="parsley-id-5043">
        <span class="parsley-required">This value is required.</span>
    </span>
</div>

I have verified using alert that the function runs. But I am new to jQuery and JavaScript, so I do not know how to debug it any further and fix it.

Kristofor answered 6/3, 2014 at 7:19 Comment(0)
F
32

You should try:

classHandler: function(el) {
    return el.$element.closest(".form-group");
}

The el is an object from parsley (I have seen it using chrome dev console) and he has a $element field with the JQuery element :)

Friedrich answered 6/3, 2014 at 10:44 Comment(2)
Can you spare a few notes about how did you look at the el object in development console?Kristofor
put a breakpoint in the classHandler function then play with "el" ;) Your breakpoint defines the scope of your function or it seems soFriedrich
M
2

Full code working:

$("#register_form").parsley({
    successClass: "has-success",
    errorClass: "has-error",
    classHandler: function(e) {
        return e.$element.closest('.form-group');
    },
    errorsWrapper: "<span class='help-block'></span>",
    errorTemplate: "<span></span>"
});
Marler answered 17/11, 2017 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.