简体   繁体   English

如何使用引导程序验证来验证所见即所得编辑器

[英]How to validate wysiwyg editor using bootstrap validation

Using 运用

bootstrap3-wysihtml5-bower 2013-11-22 (WYSIWYG Editor) bootstrap3-wysihtml5-bower 2013-11-22(所见即所得编辑器)

and

BootstrapValidator v0.5.2 BootstrapValidator v0.5.2

Validate textarea(bootstrap-wysihtml5 editor) using bootstrap validation. 使用引导程序验证来验证textarea(bootstrap-wysihtml5编辑器)。 Just need to check NotEmpty and Max Characters then submit Form. 只需检查NotEmptyMax Characters然后提交表单。

So far tried 到目前为止尝试过

 $('#setpolicyform').bootstrapValidator({ message: 'This value is not valid', feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, ignore: ":hidden:not(textarea)", fields: { policyta: { group: '.lnbrd', validators: { notEmpty: { message: 'Textarea cannot be empty' } } } } }).on('success.form.bv', function(e) { e.preventDefault(); var $form = $("#setpolicyform"), $url = $form.attr('action'); $.post($url, $form.serialize()).done(function(dte) { $("#policy-content").html(dte); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form role="form" method="POST" action="self.php" name="setpolicyform" id="setpolicyform"> <div class='box-body pad'> <div class="form-group"> <div class="lnbrd"> <textarea class="form-control textarea" name="policyta" placeholder="Place some text here" style="width: 100%; height: 200px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;"></textarea> </div> </div> </div> <div class="box-footer clearfix"> <button type="submit" class="btn btn-danger pull-right" id="setpolicyformsubmitbtn"><i class="fa fa-save"></i>&nbsp;SAVE</button> </div> </form> 

JSFiddle https://jsfiddle.net/v6s0726L/1/ JSFiddle https://jsfiddle.net/v6s0726L/1/

Folks, bootstrapValidator has been upgraded to formValidation. 伙计们,bootstrapValidator已升级为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: 如果您使用的是formValidation的更新版本,则可以这样做,而不是添加单独的类来隐藏文本区域:

   $('#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 谢谢

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 有一种方法可以验证WYSIWYG Editor ,原因是bootstrapValidator不对其进行验证,因为在textarea name="policyta"上初始化WYSIWYG Editor后, 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, 因此,一种方法是不隐藏textarea ,只需设置z-index: -1 ,它将位于WYSIWYG Editor后面,请在初始化后使用WYSIWYG Editor 事件 load来添加CSS,

CSS CSS

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

JS 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 现在,让我们使用bootstrapValidator验证文本区域,并且您还要求Max Characters限制

First bootstrapValidator script 第一个bootstrapValidator脚本

$('#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 现在,让我们使用bootstrapValidator检查并验证textarea,需要再次change wysihtml5事件以检查更改并重新验证

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 小提琴工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM