问题描述
我有一个表单,它是多步骤向导的一部分.第一步,该表单具有一个输入和一个TinyMCE编辑器.
I have a form which is part of a multi-step wizard. At one step, the form has one input and one TinyMCE editor.
我正在使用ParsleyJS来验证每个步骤的内容,然后再进行下一步.我的向导步骤和验证代码定义如下:
I'm using ParsleyJS for validating the content of each step before going to the next step. I have my wizard step and validation code defined as below:
<form class="form-horizontal" id="step1Form">
<div class="form-group">
<label for="name" class="col-sm-3 control-label">Name:</label>
<div id="nameDiv" class="col-sm-9">
<input type="text" maxlength="50" class="form-control" id="name" name="name" placeholder="Name" required="" data-parsley-group="block-1">
</div>
</div>
<div class="form-group">
<div class="col-sm-3 control-label">
<label for="consumerId">Description:</label>
</div>
<div id="descDiv" class="col-sm-9">
<div id="desc_area" data-type="textarea" data-pk="1" required="" data-parsley-group="block-1" data-parsley-tinymce="2"></div>
</div>
</div>
</form>
<script type="text/javascript">
jQuery(document).ready(function ($) {
Parsley.addValidator('tinymce', {
validateString: function(value) {
// The validation code goes here
return true;
},
messages: {
en: 'The code is invalid. This should not be shown.'
}
});
$.extend(Parsley.options, {
errorClass: 'has-error',
successClass: 'has-success',
classHandler: function(el) {
return el.$element.closest(".form-group");
},
errorsWrapper: '<span class="help-block">',
errorTemplate: '<div></div>'
});
tinymce.init ({
selector: '#desc_area',
inline: false,
force_br_newlines: false,
force_p_newlines: true,
forced_root_block: '',
menubar: false,
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code insertdatetime media contextmenu'
],
toolbar: 'insertfile undo redo | bold italic | alignleft aligncenter alignright alignjustify | outdent indent | link image'
});
});
function validateStep(step) {
if (step == 1) {
console.log("About to perform validation");
return $('#step1Form').parsley({
inputs: Parsley.options.inputs + ',[data-parsley-tinymce]'
}).validate({group: 'block-1'});
} else if (step == 2) {
// validation for step 2
} else if (step == 3) {
// validation for step 3
} else if (step == 4) {
// validation for step 4
}
return false;
}
</script>
当我单击下一步时,它会将TinyMCE编辑器标记为无效,而无需调用自定义验证器.现在,我确定我的验证器是不正确的,但是我找不到如何定义自定义验证器(当涉及到自定义验证器的定义元素时,Parsley的文档有些令人费解).
When I click next, it marks the TinyMCE editor as invalid without calling the custom validator. Now I'm sure my validator is incorrect, but I couldn't find how to define a custom validator (Parsley's documentation was a bit puzzling when it came to definition elements of custom validators).
有什么想法可以使它正常工作吗?
Any idea how I can get this to work?
推荐答案
您应该将TinyMCE绑定到< textarea>
,而不是< div>
.如果是这样,则不需要自定义验证器,只需要添加 required
或 data-parsley-required
属性.
You should be binding TinyMCE to a <textarea>
and not to a <div>
. If so, you don't need custom validators, you simply need to add the required
or data-parsley-required
attribute.
检查此工作jsfiddle ,以及以下代码:
Check this working jsfiddle, and the code below:
<form class="form-horizontal" id="step1Form">
<div class="form-group">
<label for="name" class="col-sm-3 control-label">Name:</label>
<div id="nameDiv" class="col-sm-9">
<input type="text" maxlength="50" class="form-control" id="name" name="name" placeholder="Name" required="" data-parsley-group="block-1">
</div>
</div>
<div class="form-group">
<div class="col-sm-3 control-label">
<label for="consumerId">Description:</label>
</div>
<div id="descDiv" class="col-sm-9">
<textarea id="desc_area" data-type="textarea" data-pk="1" required="" data-parsley-group="block-1"></textarea>
</div>
</div>
</form>
<script>
jQuery(document).ready(function ($) {
$.extend(Parsley.options, {
errorClass: 'has-error',
successClass: 'has-success',
classHandler: function(el) {
return el.$element.closest(".form-group");
},
errorsWrapper: '<span class="help-block">',
errorTemplate: '<div></div>'
});
tinymce.init ({
selector: '#desc_area',
inline: false,
force_br_newlines: false,
force_p_newlines: true,
forced_root_block: '',
menubar: false,
plugins: [
'advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code insertdatetime media contextmenu'
],
toolbar: 'insertfile undo redo | bold italic | alignleft aligncenter alignright alignjustify | outdent indent | link image'
});
// NOTE: This is only here for testing purposes.
// Somewhere in your code you should be validating each block
$("form").parsley();
$("form").on('submit', function(e) {
e.preventDefault();
$(this).parsley().validate();
if ($(this).parsley().isValid()) {
alert('ok');
} else {
alert('crap!');
}
})
});
</script>
这篇关于使用Parsley验证TinyMCE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!