我有一个表格,它是多步骤向导的一部分。一步,表单有一个输入和一个TinyMCE编辑器。
我正在使用ParsleyJS验证每个步骤的内容,然后再进行下一步。我的向导步骤和验证代码定义如下:
<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的文档在定制自定义验证器的定义元素时有点令人费解。)
知道如何才能让它发挥作用吗?
答案 0 :(得分:2)
您应该将TinyMCE绑定到<textarea>
而不是<div>
。如果是这样,您就不需要自定义验证程序,只需添加required
或data-parsley-required
属性即可。
检查此working jsfiddle和以下代码:
<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>