Skip to content

Commit

Permalink
提高容错性,增加允许为空的快捷定义支持,详见更新日志
Browse files Browse the repository at this point in the history
  • Loading branch information
CodePlayer committed Aug 24, 2020
1 parent 0a527c6 commit c02ef77
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 1.3.7

* 【新增】 在为每个表单元素指定验证规则时,支持直接在现有规则名称后直接加后缀 "?", 表示允许当前表单元素为空(相当于强制添加了 `required: false` )。
* 【新增】 `V.fn.isRequired(value, context)` 方法API,允许重写该方法以自定义实现如何指示当前元素是否允许为空。请注意:只有返回明确的 `true``false` 时,才会设置允许对应的表单元素是否 非空 或 允许为空,其他任何返回值均 **不影响** 原规则校验。

默认实现下,如果表单元素上存在属性(attribute) `optional`,其值为 `true` 则表示允许当前表单元素为空(也相当于强制添加了 `required: false` );其值为 `false` 则表示该表单元素不能为空(相当于强制添加了 `required: true` );其他任何值均不作处理。
* 【优化】 非严格模式下,调整表单验证的底层代码执行顺序,当表单元素不存在时,如果对应的规则名称未定义也不会报错。
5 changes: 2 additions & 3 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
<meta http-equiv="expires" content="0">
</head>
<body>
<form action="http://www.365mini.com/">
<form action="https://codeplayer.vip/">
<div><label>用户名</label><input id="username" name="username" type="text" /></div>
<div><label>密码</label><input id="password" name="password" type="password" /></div>
<div><label>年龄</label><input id="age" name="age" type="text" /></div>
<div><label>出生日期</label><input id="birthday" name="birthday" type="text" /></div>
<div><label>手机号码</label><input id="cellphone" name="cellphone" type="text" /></div>
<div><label>兴趣爱好</label><input name="hobby" type="checkbox" value="0" /> 足球 <input name="hobby" type="checkbox" value="1" />篮球 </div>
<input type="submit" value="提交" />
</form>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
</form><script type="text/javascript" src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="layer/layer.js"></script>
<script type="text/javascript" src="../jquery-validator.js"></script>
<script type="text/javascript">
Expand Down
53 changes: 36 additions & 17 deletions jquery-validator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**************************************
@Name: jQuery-Validator 基于jQuery的前端验证框架
@Version: 1.3.6
@Version: 1.3.7
@Author: Ready
@Date: 2018-08-01
@Documentation: http://www.365mini.com/page/jquery-validator-quickstart.htm
@Copyright: CodePlayer( Ready )
@Date: 2020-08-24
@Documentation: https://codeplayer.vip/p/j7sud
@Email: [email protected]
@Licence: https://www.apache.org/licenses/LICENSE-2.0.html
*/
Expand Down Expand Up @@ -101,9 +100,18 @@
},
// 后置处理
afterHandler: function(result, context){
var handler = context.rule.after;
var handler = context.rule && context.rule.after;
if(handler && $.isFunction(handler)) handler.call(this, result, context);
return result;
},
/**
* 指示当前元素是否必须非空(只有对表单元素进行校验时,才会被框架调用)。只有返回 true 或 false 时,才会设置允许对应的表单元素是否 非空 或 允许为空,其他任何返回值均不影响原规则校验
*/
isRequired: function(val, context){
var optional = context.$dom.attr('optional');
if( optional == 'true' || optional == 'false' ){
return optional == 'true';
}
},
// 元素值预处理器,必须返回值
pre: {
Expand Down Expand Up @@ -513,20 +521,31 @@
},
// 执行单个校验
validate: function(value, rule, event){
var me = this, context, is$, done;
var me = this, context, is$ = value instanceof $, ruleName, $dom = is$ ? value : null, done, required;
if( typeof rule === "string" ){
context = me.getRule(rule);
if( !context ) throw "validate rule not found:" + rule;
rule = context;
ruleName = rule;
if(ruleName.slice(-1) == '?'){
ruleName = ruleName.slice(0, -1);
required = false;
}
rule = me.getRule(ruleName);
}
context = V.context = { origin: rule };
rule = context.rule = me.clipRule( rule );
is$ = value instanceof $;
V.debug && log( "current validate context [" + (is$ ? value.prop("name") : value) + "]:" , context );
context = V.context = { ruleName: ruleName, origin: rule, $dom: $dom, rule: rule };
V.debug && log( "current validate context [" + (is$ ? $dom.selector : value) + "]:" , context );
if( is$ ){
if(!value.length && !this.strict) return me.afterHandler(true, context); // 非严格模式,直接跳过校验
context.$dom = value;
value = me.getValue(value, context);
if(!$dom.length && !me.strict) return me.afterHandler(true, context); // 非严格模式,直接跳过校验
value = me.getValue($dom, context);
if( required == null ){
var r = me.isRequired(value, context);
if( typeof r === 'boolean' ){
required = r;
}
}
}
if( !rule ) throw "validate rule not found:" + ruleName;
rule = context.rule = me.clipRule( rule );
if( required != null ){
rule.required = required;
}
context.value = value, done = { };
// 如果设置了条件预过滤器,只有在匹配条件时才执行后续校验
Expand Down Expand Up @@ -676,7 +695,7 @@
},
// 渲染错误
renderError: function(message, $target, context){
if($target && $.isFunction($target.tips)){
if($target && $target.length && $.isFunction($target.tips)){
$target.tips(message);
}else {
alert(message);
Expand Down
Loading

0 comments on commit c02ef77

Please sign in to comment.