How to validate wysiwyg editor using bootstrap validation

后端 未结 2 1772
暗喜
暗喜 2021-01-20 15:56

Using

bootstrap3-wysihtml5-bower 2013-11-22 (WYSIWYG Editor)

and

BootstrapValidator v0.5.2

相关标签:
2条回答
  • 2021-01-20 16:17

    There is a way to validate the WYSIWYG Editor, reason bootstrapValidator not validating it because after initializing WYSIWYG Editor on textarea name="policyta", it will be hidden and ignored by bootstrapValidator

    So the one way is do not hide the textarea, just set z-index: -1 and it will go behind the WYSIWYG Editor, use WYSIWYG Editor event load to add the CSS after initializing,

    CSS

    .textnothide {
        display: block !important;
        position: absolute;
        z-index: -1;
    }
    

    JS

    $('.textarea').wysihtml5({
        events: {
            load: function () {
                $('.textarea').addClass('textnothide');
            }
        }
    });
    

    Now let's validate the textarea with bootstrapValidator and you also asked for Max Characters limit

    First bootstrapValidator script

    $('#setpolicyform').bootstrapValidator({
        message: 'This value is not valid',
        //ignore: ":hidden:not(textarea)", //<---- No Need of This
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            policyta: {
                group: '.lnbrd',
                validators: {
                    notEmpty: {
                        message: 'Textarea cannot be empty'
                    },
                    stringLength: { //<- Limit Maximum Character(s)
                        max: 50,
                        message: 'Maximum 50 Characters Required'
                    }
                }
            }
        }
    });
    

    Now let's check and validate the textarea with bootstrapValidator, need another wysihtml5 event change to check the changes and re-validate

    change: function () {
        $('#setpolicyform').bootstrapValidator('revalidateField', 'policyta');
    }
    

    So the Final Script will be

    $(document).ready(function () {
        $('#setpolicyform').bootstrapValidator({
            message: 'This value is not valid'
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                policyta: {
                    group: '.lnbrd',
                    validators: {
                        notEmpty: {
                            message: 'Textarea cannot be empty'
                        },
                        stringLength: {
                            max: 50,
                            message: 'Maximum 50 Characters Required'
                        }
                    }
                }
            }
        });
        $('.textarea').wysihtml5({
            events: {
                load: function () {
                    $('.textarea').addClass('textnothide');
                },
                change: function () {
                    $('#setpolicyform').bootstrapValidator('revalidateField', 'policyta');
                }
            }
        });
    });
    

    Fiddle Working Example

    0 讨论(0)
  • 2021-01-20 16:33

    Folks, bootstrapValidator has been upgraded to formValidation. If you are using the updated version of formValidation you can do this instead of adding a separate class to hide the text area:

       $('#setpolicyform').formValidation({
                                    framework: 'bootstrap',
                                    excluded: [':disabled'], /* This will do the trick of validating for notEmpty*/
                                    icon : {
                                        valid : '',
                                        invalid : '',
                                        validating : 'glyphicon glyphicon-refresh'
                                    },
                                    fields: {
                                     policyta: {
                                      group: '.lnbrd',
                                      validators: {
                                       notEmpty: {
                                       message: 'Textarea cannot be empty'
                                       },
                                       stringLength: {
                                        max: 50,
                                       message: 'Maximum 50 Characters Required'
                                   }
                                }
                             }
                          }
            });
    
    $('.textarea').wysihtml5({
            events: {
                change: function () {
                    $('#setpolicyform').formValidation('revalidateField', 'policyta');
            }
        }
    });
    

    Thanks

    0 讨论(0)
提交回复
热议问题