Skip to content
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

Closed
rubyisapm opened this issue Aug 26, 2016 · 5 comments
Closed

avalon2对于非必填项的验证 #1708

rubyisapm opened this issue Aug 26, 2016 · 5 comments

Comments

@rubyisapm
Copy link

如果一个input是非必填的,且只能为数字,大于0等限制。avalon2的验证机制仍会对为空时进行规则判断。建议添加required:false 或其他可以解决该类问题的方案。

@RubyLouvre
Copy link
Owner

不是必填的,它也不用检测是什么内容吧!

@rubyisapm
Copy link
Author

一个字段可以不填,但如果要填的话一定要是合法的数字

@RubyLouvre
Copy link
Owner

那自己自定义规则

@RubyLouvre
Copy link
Owner

<!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>

@rubyisapm
Copy link
Author

嗯呢,我有在用自定义规则的,谢谢老师啦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants