parsley 2.0.3 not working with boostrap 3
Asked Answered
C

1

0

i am trying to use parsely.js on my html page to validate input box. currently this html page contains one input box and one submit button. the structure is created using bootstrap 3 and this page does not contain Form tag.

<div role='form'>
    <div class="row form-group">
        <div class="col-xs-3">
            <label title="fullname">Full Name</label>
        </div>
        <div class="col-xs-4">
            <input type="text" class='form-control' id="name" name="fullName" data-parsley-required="true" data-parsley-required-message="Please insert your name"/>
        </div>
    </div>

  <input type="submit"  class= "btn btn-danger"/> </div>

i am calling parsley.js like

function validateInput()
        {

        var handle = $("input[name='fullName']").parsley({
            successClass: "has-success",
            errorClass: "has-error",
            classHandler: function (el) {
              return $(el).closest('.form-group');//not working

            },
            errorsWrapper: "<span class='help-block'></span>",
            errorTemplate: "<span></span>",

        });

        return handle.isValid();
    }

on click of Submit button. it returns true/false correctly and create span tag also. but error classes are not applied. even data-parsley-required-message'Please insert your name' is not working.

when i put alert($(el)) or alert(el) it gives [object Object]. i think el should be the input object on which i am calling parsley function. but i am not able to get el.attr('id') or any other attribute. it returns undefined. i have also tried

//return el.closest('.form-group');//not working
//return el.$element.closest('.form-group)//not working
//return $(el).$element.closest('.form-group')//not working

I can not use Form tag as i am using this html structure in sharepoint content edtior web part.

Coff answered 26/7, 2014 at 18:51 Comment(0)
L
4

A few things first:

  1. Parsley allows you to bind it to a field, so you won't have a problem without the form element (see docs);

  2. The classHandler function recieves an object of the type ParsleyField. With this object, you can access the input element with el.$element (for example: alert(el.$element.attr('id'));

I have made the following changes to your validateInput function:

<script type="text/javascript">
    function validateInput() {
        $("input[name='fullName']").parsley({
            successClass: "has-success",
            errorClass: "has-error",
            classHandler: function (el) {
                return el.$element.closest('.form-group'); //working
            },
            errorsWrapper: "<span class='help-block'></span>",
            errorTemplate: "<span></span>",
        });

        // Returns true / false if the field has been validated. Does not affect UI.
        //$("input[name='fullName']").parsley().isValid());

        // validate field and affects UI
        $("input[name='fullName']").parsley().validate();
    }
</script>

With this code, the message is presented correctly, and the successClass and errorClass are appended to the div form-group.

See the following working jsfiddle

Lanny answered 26/7, 2014 at 19:33 Comment(1)
This is great, working for me, how do you remove the class "help-block" once the error has been corrected by the user. I know parsley will remove the text, but the surrounding div has some padding in which is left visible once corrected... any suggestions?Timekeeper

© 2022 - 2024 — McMap. All rights reserved.