New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
avalon2对于非必填项的验证 #1708
Comments
不是必填的,它也不用检测是什么内容吧! |
一个字段可以不填,但如果要填的话一定要是合法的数字 |
那自己自定义规则 |
<!DOCTYPE html>
<html>
<head>
<title>ms-validate</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="../dist/avalon.js"></script>
<script>
avalon.validators.aaa = {
message: '必须数字并大于0',
get: function (value, field, next) {//数值
var ok = (value === '' || (Number(value) > 0))
next(ok)
return value
}
}
var vm = avalon.define({
$id: "test",
aaa: '',
validate: {
onError: function (reasons) {
reasons.forEach(function (reason) {
console.log(reason.getMessage())
})
},
onValidateAll: function (reasons) {
if (reasons.length) {
console.log('有表单没有通过')
} else {
console.log('全部通过')
}
}
}
})
</script>
</head>
<body ms-controller="test">
<form class="cmxform" ms-validate="@validate" >
<fieldset>
<legend>自定义规则</legend>
<p>
<input
ms-duplex="@aaa"
ms-rules="{aaa: true}"
>
</p>
</fieldset>
<p>
<input class="submit" type="submit" value="提交">
</p>
</fieldset>
</form>
</body>
</html> |
嗯呢,我有在用自定义规则的,谢谢老师啦 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
如果一个input是非必填的,且只能为数字,大于0等限制。avalon2的验证机制仍会对为空时进行规则判断。建议添加required:false 或其他可以解决该类问题的方案。
The text was updated successfully, but these errors were encountered: