【问题标题】:Force an error on a field with Parsley.js使用 Parsley.js 在字段上强制出错
【发布时间】:2016-07-01 19:45:38
【问题描述】:

我正在使用 parsley (1.x) 对表单进行客户端验证。在服务器端验证后,我需要在某个字段上强制出错。

<form>
<input type="text" name="field1" >
<input type="text" name="field2" >
<input type="submit" value="Send">
</form>

我已经尝试了此页面中描述的方法,但它不起作用: https://github.com/guillaumepotier/Parsley.js/issues/111

谁能帮帮我?

谢谢

【问题讨论】:

  • 您能否阐明您的要求。你在使用 JavaScript 吗?您希望“在服务器端验证后”出现错误 - 大概是服务器端代码检测到错误并重新显示您希望显示错误的页面。服务器将如何指示错误?
  • 它进行一个ajax调用,返回一个带有服务器响应的JSon。我已经过滤了结果,并且能够找到包含错误的输入名称。我想要这样的东西: $("#inputWithError").parsley('addError','Error message');
  • 您可以使用parsley-remote="http://yoururl.com" 进行ajax 调用吗?请参阅 v1 文档:parsleyjs.github.io/Parsley-1.x/documentation.html

标签: parsley.js


【解决方案1】:

在 2.4.3 版本中,您可以这样做:

$('#fieldName').parsley().addError('forcederror', {message: 'My error message.', updateClass: true});

来源:http://parsleyjs.org/doc/index.html#psly-ui-for-javascript

【讨论】:

    【解决方案2】:

    我已经设法解决了这个问题,编写了一个新的验证规则,该规则返回一个无效的字段。

    forcevalidation : function() {
                        return {
                            validate : function(val, elem, self) {
                                return false;
                            },
                            priority : 32
                        };
                    },
    

    当我收到 ajax 响应时,我将验证器添加到受影响的字段,然后将其删除:

                    affectedInput.attr('parsley-forcevalidation', true);
                    affectedInput.attr('parsley-error-message', errorMessage);
                    affectedInput.parsley('destroy');
                    affectedInput.parsley('validate');
                    affectedInput.removeAttr('parsley-forcevalidation');
    

    【讨论】:

      猜你喜欢
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 2015-05-28
      相关资源
      最近更新 更多