Parsley.js - Displaying Errors in a Specified Element

前端 未结 6 1980
Happy的楠姐
Happy的楠姐 2020-12-30 08:01

Using Parsley.js, is it possible to specify the element that should hold the error messages? I tried:

$(\'#myForm\').parsley({
    errors: {
        containe         


        
相关标签:
6条回答
  • 2020-12-30 08:32

    I returned true from the function provided with container key.

    My HTML Element

    <input type="text" class="input-small" errorSpan="yyy"  id="ffffd" name="ffffd" value="" data-required="true">
    <span id="yyy"></span>
    

    Javascript

    $('#abc').parsley({
                errors: {
                    classHandler: function ( elem ) {}
                  , container: function ( elem, template, isRadioOrCheckbox ) {
                       //here i have span msg. id to be displayed as custom attribute in input element
                        $('#' + $(elem).attr('errorSpan')).html(template);
                        return true;//returning back boolean makes it work
                      }
                  , errorsWrapper: '<ul></ul>'
                  , errorElem: '<li></li>'
                  }
            });
    

    It also works if I return

    return $('#' + $(elem).attr('errorSpan')).html(template);
    

    Hope this helps......

    0 讨论(0)
  • 2020-12-30 08:35

    You'll need to use a callback function to do so

    Here a simple example to attach error messages to element parent for example.

    $('#myForm').parsley({
        errors: {
            container: function ( elem ) {
                return $( elem ).parent();
            }
        }
    });
    

    EDIT: Working on 1.1.10-dev, I changed the way to define the errors container like above. Careful, this is a BC Break;

    0 讨论(0)
  • 2020-12-30 08:37

    I've added another data-attribute, data-parsley-errors-container="#element". That could allow you modify the DOM to specify where the error messages will be displayed.

    More info here: http://parsleyjs.org/doc/index.html#ui-for-field

    Best

    0 讨论(0)
  • 2020-12-30 08:39

    Just define classHandler function before Parsley library import, in my case, i'm using bootstrap and it's to apply error and valid classes on parent div (it has "form-group" class).

    <script>
        window.ParsleyConfig = {
          classHandler: function ( parsleyField) {
          // specify where parsley error-success classes will be set
          return parsleyField.$element.parent();
          // Change this to change the element to apply too
          },
        };
    </script>
    

    Now just add data-parsley validate to your form tag :

    <form  method="post" id="form1" data-parsley-validate>
    

    When using bootstrap, you need to specify bootstrap error and valid classes too

    <form method="post" id="form1" data-parsley-error-class="has-error" data-parsley-success-class="has-success" data-parsley-validate>
    

    In order to get the "has-error" class set on the parent div on invalid/error state (same for valid too):

    <div class="form-group has-error">
        <label>Name</label>
        <input class="form-control" type="text" id="name" name="name" required="" data-parsley-trigger="keyup" data-parsley-minlength="5">
        <p class="help-block">Example help text here.</p>
    </div>
    

    This solution global to all fields.

    0 讨论(0)
  • 2020-12-30 08:55

    data-parsley-errors-container="#your-div-id" worked for me

    <div class="form-group">
      <label for="middle-name" class="control-label col-md-3 col-sm-3 col-xs-12">Start Time</label>
      <div class="col-md-6 col-sm-6 col-xs-12">
           <div class=" datetimepicker3 input-append timepick">
              <input class="form-control" data-format="hh:mm" placeholder="HH:MM" type="text" name="startTime" data-parsley-errors-container="#startTimeErrorContainer" required="required"  id="startTime" />
              <span class="add-on"><i class="fa fa-clock-o icon-time"></i></span>  
           </div>   
          <div id="startTimeErrorContainer"></div>                                            
      </div>                       
    

    0 讨论(0)
  • 2020-12-30 08:58

    @guillaumepotier I have try your code on jquerymobile and I do not show any error message display on screen. I want to add error-message to class "help-inline"

    index.html

    <script src="js/vendor/parsley.message.th.js"></script>
    <script src="js/vendor/parsley.extend.min.js"></script>
    <script src="js/vendor/parsley.min.js"></script>
    
    ...
    
    <div class="control-group">
        <label class="control-label" for="A0_C1">From<i class="required-icon"></i></label>
        <div class="controls">
            <input type="text" id="A0_C1" name="A0_C1" value="" required="required" />
            <span class="help-inline"></span>
        </div>
    </div>
    

    parsley.message.th.js

    window.ParsleyConfig = $.extend( true, {}, window.ParsleyConfig, { 
        errors: {
            container: function ( elem ) {
                return $(elem).parent().find(".help-inline");
            }
        }
    });
    

    UPDATE - Working solution on v1.1.9-dev

    return $(elem).closest('.controls').find('.help-inline').append(template);
    
    0 讨论(0)
提交回复
热议问题