• vue view 表单验证正常逻辑


    <template>
        <Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
            <FormItem prop="user">
                 <InputNumber :max="100" :min="0" step="1" v-model="formInline.user"></InputNumber>
            </FormItem>
            <FormItem prop="password">
               <InputNumber :max="100" :min="0" step="1" v-model="formInline.password"></InputNumber>
            </FormItem>
            <FormItem>
                <Button type="primary" @click="handleSubmit('formInline')">登录</Button>
            </FormItem>
        </Form>
    </template>
    <script>
        export default {
            data () {
               const user = (rule, value, callback) => {
                    if (value == 0) {
                        callback(new Error('用户名不能为空'));
                    } else {
                        callback();
                    }
                };
                const password = (rule, value, callback) => {
                    if (value == 0) {
                        callback(new Error('密码不能为空'));
                    } else {
                        callback();
                    }
                };
                return {
                    formInline: {
                        user: 0,
                        password: 0
                    },
                    ruleInline: {
                        user: [
                            { required: true, validator: user, trigger: 'change' }
                        ],
                        password: [
                            { required: true, validator: password, trigger: 'change' },
                        ]
                    }
                }
            },
            methods: {
                handleSubmit(name) {
                    this.$refs[name].validate((valid) => {
                        if (valid) {
                            this.$Message.success('Success!');
                        } else {
                            this.$Message.error('Fail!');
                        }
                    })
                }
            }
        }
    </script>
    <template>
    <Form ref="formInline" :model="formInline" :rules="ruleInline" inline>
    <FormItem prop="user">
    <InputNumber :max="100" :min="0" step="1" v-model="formInline.user"></InputNumber>
    </FormItem>
    <FormItem prop="password">
    <InputNumber :max="100" :min="0" step="1" v-model="formInline.password"></InputNumber>
    </FormItem>
    <FormItem>
    <Button type="primary" @click="handleSubmit('formInline')">登录</Button>
    </FormItem>
    </Form>
    </template>
    <script>
    export default {
    data () {
    const user = (rule, value, callback) => {
    if (value == 0) {
    callback(new Error('用户名不能为空'));
    } else {
    callback();
    }
    };
    const password = (rule, value, callback) => {
    if (value == 0) {
    callback(new Error('密码不能为空'));
    } else {
    callback();
    }
    };
    return {
    formInline: {
    user: 0,
    password: 0
    },
    ruleInline: {
    user: [
    { required: true, validator: user, trigger: 'change' }
    ],
    password: [
    { required: true, validator: password, trigger: 'change' },
    ]
    }
    }
    },
    methods: {
    handleSubmit(name) {
    this.$refs[name].validate((valid) => {
    if (valid) {
    this.$Message.success('Success!');
    } else {
    this.$Message.error('Fail!');
    }
    })
    }
    }
    }
    </script>
  • 相关阅读:
    Cache,ViewState,Session,Application,Static变量
    Java -- 通过 URLConnection 进行http请求中文乱码
    maven
    SpringMVC restful风格下载文件,文件后缀被截掉
    vim 命令
    CentOS -- 命令
    json -- fastjson如何序列化@Transient的字段
    SpringBoot -- 配置mysql、hibernate
    Java -- 泛型父类中获取子类的泛型T
    Excel
  • 原文地址:https://www.cnblogs.com/xkloveme/p/7906612.html
Copyright © 2020-2023  润新知