Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erroneous placement of error message span with Parsley 2.x and Bootstrap 3

I'm trying to add Parsley validation to a set of radio buttons contained within a Bootstrap 3 btn-group.

The issue is that the error wrapper that parsley injects (in my case set to a span) is appearing in between the two options (which are contained within a label) rather that outside the btn-group div.

This is illustrated in the sample below:

<div class="form-group">
    <div class="btn-group" data-toggle="buttons">
        <label class="btn btn-default"><input type="radio" name="BuyAgain" value="True" data-parsley-required="true" data-parsley-multiple="BuyAgain" data-parsley-id="3282">Yes</label>
        <span class="help-block" id="parsley-id-multiple-BuyAgain"></span>
        <label class="btn btn-default"><input type="radio" name="BuyAgain" value="False" data-parsley-required="true" data-parsley-multiple="BuyAgain" data-parsley-id="3282">No</label>
    </div>
</div>

Any suggestions as to how I can remedy this?

like image 618
Jon Avatar asked Mar 25 '14 17:03

Jon


2 Answers

After digging through the source code, I noticed there was an "errorsContainer" option available when initialising Parsley.

After changing the initalisation function to:

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

I now get the resulting html:

<div class="form-group">
    <div class="btn-group" data-toggle="buttons">
        <label class="btn btn-default"><input type="radio" name="BuyAgain" value="True" data-parsley-required="true" data-parsley-multiple="BuyAgain" data-parsley-id="1481">Yes</label>
        <label class="btn btn-default"><input type="radio" name="BuyAgain" value="False" data-parsley-required="true" data-parsley-multiple="BuyAgain" data-parsley-id="1481">No</label>
    </div>
    <span class="help-block" id="parsley-id-multiple-BuyAgain"></span>
</div>
like image 71
Jon Avatar answered Oct 07 '22 21:10

Jon


Another response, which is equivqlent to @Jon response is to add a the data-parsley-errors-container attribute to your form.

<form data-parsley-errors-container=".form-group" data-parsley-validate="">
    ...
</form>
like image 39
Moebius Avatar answered Oct 07 '22 23:10

Moebius