Summernote与Jquery Validation插件的验证

huangapple go评论78阅读模式
英文:

Summernote validation with Jquery Validation plugin

问题

  1. 我有一个表单其中有一个远程验证字段我的JavaScript代码如下
  2. ```javascript
  3. $("#myform").validate({
  4. rules: {
  5. mainfield: {
  6. remote: {
  7. url: "checksnote.php",
  8. type: "POST",
  9. data: {
  10. mainfield: function() {
  11. return $("#mainfield").summernote('isEmpty');
  12. }
  13. }
  14. }
  15. }
  16. },
  17. messages: {
  18. mainfield: {
  19. remote: "此字段为必填项",
  20. //minlength: "最小长度为10"
  21. }
  22. },
  23. submitHandler: function(form) {
  24. alert("提交成功");
  25. console.log("提交成功");
  26. form.submit();
  27. }
  28. });

问题是我无法得到任何响应。远程请求都显示有效,但浏览器中看不到任何Ajax请求。Mainfield是summernote文本区域。所有其他验证都正常工作。为什么远程请求甚至不发送请求?

更新-
我也尝试过添加方法,但不起作用。这就是为什么我尝试使用remote。如果有任何一个起作用都可以。浏览器没有显示任何错误。

  1. $.validator.addMethod('summernoteempty', function(value, element) {
  2. if ($(element).summernote('isEmpty')) {
  3. console.log("在summernote验证中!");
  4. return false;
  5. } else {
  6. return true;
  7. }
  8. }, "必须输入summernote内容");

规则如下:

  1. rules: {
  2. mainfield: {
  3. summernoteempty: true
  4. }
  5. },
  6. messages: {
  7. summernoteempty: "字段不能为空"
  8. }

我不明白我在这里做错了什么。它甚至不在控制台中显示消息。除了remote和add method之外,所有其他验证逻辑都正常工作。

  1. <details>
  2. <summary>英文:</summary>
  3. I have a form with a remote validation field. My Javascript is as bellow:
  4. $(&quot;#myform&quot;).validate({
  5. rules: {
  6. mainfield: {
  7. remote:{
  8. url: &quot;checksnote.php&quot;,
  9. type: &quot;POST&quot;,
  10. data: {
  11. mainfield: function() {
  12. return $(&quot;#mainfield&quot;).summernote(&#39;isEmpty&#39;);
  13. }
  14. }
  15. }
  16. }
  17. },
  18. messages:{
  19. mainfield: {
  20. remote: &quot;This field is required&quot;,
  21. //minlength: &quot;Minimum Length is 10&quot;
  22. }
  23. },
  24. submitHandler: function(form) {
  25. alert(&quot;Successfully submittable&quot;);
  26. console.log(&quot;Successfully submittable&quot;);
  27. form.submit();
  28. }
  29. });
  30. 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?
  31. **Update-**
  32. I also tried to add Method did not work. Thats why I tried remote instead. Either one is fine if works for me.
  33. This is my method code. Browser is not showing any error.
  34. $.validator.addMethod(&#39;sumnoteempty&#39;, function(value,element){
  35. //return false;
  36. if(element.summernote(&#39;isEmpty&#39;)){
  37. console.log(&quot;In summernote validation!&quot;);
  38. return false;
  39. }
  40. else {
  41. return true;
  42. }
  43. }, &quot;Must enter summernote content&quot;);
  44. Rules are as bellow:
  45. rules: {
  46. mainfield: {
  47. sumnoteempty: true
  48. }
  49. },
  50. message:{
  51. sumnoteempty: &quot;Field can not be empty&quot;
  52. }
  53. 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.
  54. </details>
  55. # 答案1
  56. **得分**: 1
  57. 不需要查询服务器来评估字段是否为空。创建一个自定义规则,使用Summernote的`isEmpty`方法。
  58. ```javascript
  59. jQuery.validator.addMethod("requiredSummernote", function() {
  60. // true to pass validation
  61. // false to FAIL validation
  62. return !($("#mainfield").summernote('isEmpty'));
  63. }, 'Summernote field is required');
  64. $("#myform").validate({
  65. rules: {
  66. mainfield: { // name attribute
  67. requiredSummernote: true; // apply the custom rule
  68. }
  69. },
  70. ....
  71. });

由于Summernote隐藏了原始的textarea元素并构建了自己的元素,因此您需要执行以下几个操作以将它们整合在一起:

  • 编辑Validate插件的ignore选项,以强制验证隐藏的textarea元素。默认情况下,隐藏元素会被忽略。

  • 使用errorPlacement选项来更改验证消息出现在屏幕上的位置。它可能会出现在不合适的位置,因为实际的数据输入元素是隐藏的。

  • 您可能需要构建处理程序,以以编程方式触发验证。由于实际的数据元素是隐藏的,所以需要使用blurchange处理程序以及.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.

  1. jQuery.validator.addMethod(&quot;requiredSummernote&quot;, function() {
  2. // true to pass validation
  3. // false to FAIL validation
  4. return !($(&quot;#mainfield&quot;).summernote(&#39;isEmpty&#39;));
  5. }, &#39;Summernote field is required&#39;);
  6. $(&quot;#myform&quot;).validate({
  7. rules: {
  8. mainfield: { // name attribute
  9. requiredSummernote: true; // apply the custom rule
  10. }
  11. },
  12. ....

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 hidden textarea 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 a blur or change handler and the
    .valid() method which forces a test.

  • Conversely, if the error message will not go away, construct a
    handler that is triggered on blur that calls .valid() and then
    hides the label element containing the error message.

huangapple
  • 本文由 发表于 2023年7月20日 16:38:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定