英文:
Summernote validation with Jquery Validation plugin
问题
我有一个表单,其中有一个远程验证字段。我的JavaScript代码如下:
```javascript
$("#myform").validate({
rules: {
mainfield: {
remote: {
url: "checksnote.php",
type: "POST",
data: {
mainfield: function() {
return $("#mainfield").summernote('isEmpty');
}
}
}
}
},
messages: {
mainfield: {
remote: "此字段为必填项",
//minlength: "最小长度为10"
}
},
submitHandler: function(form) {
alert("提交成功");
console.log("提交成功");
form.submit();
}
});
问题是我无法得到任何响应。远程请求都显示有效,但浏览器中看不到任何Ajax请求。Mainfield是summernote文本区域。所有其他验证都正常工作。为什么远程请求甚至不发送请求?
更新-
我也尝试过添加方法,但不起作用。这就是为什么我尝试使用remote。如果有任何一个起作用都可以。浏览器没有显示任何错误。
$.validator.addMethod('summernoteempty', function(value, element) {
if ($(element).summernote('isEmpty')) {
console.log("在summernote验证中!");
return false;
} else {
return true;
}
}, "必须输入summernote内容");
规则如下:
rules: {
mainfield: {
summernoteempty: true
}
},
messages: {
summernoteempty: "字段不能为空"
}
我不明白我在这里做错了什么。它甚至不在控制台中显示消息。除了remote和add method之外,所有其他验证逻辑都正常工作。
<details>
<summary>英文:</summary>
I have a form with a remote validation field. My Javascript is as bellow:
$("#myform").validate({
rules: {
mainfield: {
remote:{
url: "checksnote.php",
type: "POST",
data: {
mainfield: function() {
return $("#mainfield").summernote('isEmpty');
}
}
}
}
},
messages:{
mainfield: {
remote: "This field is required",
//minlength: "Minimum Length is 10"
}
},
submitHandler: function(form) {
alert("Successfully submittable");
console.log("Successfully submittable");
form.submit();
}
});
Problem is I am unable to get any response at all. Both the remote requests show valid but no ajax request is seen in the browser. Mainfield is summernote textarea. All other validations are working perfectly fine. Any idea why remote is not even sending a request?
**Update-**
I also tried to add Method did not work. Thats why I tried remote instead. Either one is fine if works for me.
This is my method code. Browser is not showing any error.
$.validator.addMethod('sumnoteempty', function(value,element){
//return false;
if(element.summernote('isEmpty')){
console.log("In summernote validation!");
return false;
}
else {
return true;
}
}, "Must enter summernote content");
Rules are as bellow:
rules: {
mainfield: {
sumnoteempty: true
}
},
message:{
sumnoteempty: "Field can not be empty"
}
I dont understand what I am doing wrong here. It does not even show message in console. All other validation logic is working great. Except remote and add method.
</details>
# 答案1
**得分**: 1
不需要查询服务器来评估字段是否为空。创建一个自定义规则,使用Summernote的`isEmpty`方法。
```javascript
jQuery.validator.addMethod("requiredSummernote", function() {
// true to pass validation
// false to FAIL validation
return !($("#mainfield").summernote('isEmpty'));
}, 'Summernote field is required');
$("#myform").validate({
rules: {
mainfield: { // name attribute
requiredSummernote: true; // apply the custom rule
}
},
....
});
由于Summernote隐藏了原始的textarea
元素并构建了自己的元素,因此您需要执行以下几个操作以将它们整合在一起:
-
编辑Validate插件的
ignore
选项,以强制验证隐藏的textarea
元素。默认情况下,隐藏元素会被忽略。 -
使用
errorPlacement
选项来更改验证消息出现在屏幕上的位置。它可能会出现在不合适的位置,因为实际的数据输入元素是隐藏的。 -
您可能需要构建处理程序,以以编程方式触发验证。由于实际的数据元素是隐藏的,所以需要使用
blur
或change
处理程序以及.valid()
方法来强制进行测试。 -
相反,如果错误消息不会消失,请构建一个在
blur
上触发的处理程序,该处理程序调用.valid()
,然后隐藏包含错误消息的label
元素。
英文:
There is no need to query the server to evaluate when a field is left empty. Create a custom rule that uses the Summernote isEmpty
method.
jQuery.validator.addMethod("requiredSummernote", function() {
// true to pass validation
// false to FAIL validation
return !($("#mainfield").summernote('isEmpty'));
}, 'Summernote field is required');
$("#myform").validate({
rules: {
mainfield: { // name attribute
requiredSummernote: true; // apply the custom rule
}
},
....
https://jqueryvalidation.org/jQuery.validator.addMethod/
Since Summernote hides the original textarea
element and constructs its own, you will need to do several other things to pull this all together:
-
Edit the
ignore
option of the Validate plugin in order to force
validation of the hiddentextarea
element. Hidden elements are
ignored by default. -
Use the
errorPlacement
option to alter where the validation message
appears on the screen. It may be out of place since the actual data
input element is hidden. -
You may need to construct handlers that programmatically trigger
validation. Since the actual data element is hidden, validation
needs to be triggered with ablur
orchange
handler and the
.valid()
method which forces a test. -
Conversely, if the error message will not go away, construct a
handler that is triggered onblur
that calls.valid()
and then
hides thelabel
element containing the error message.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论