avalon前端系列(一)-表单验证

背景:公司需要做一个兼容性比较好的前端PC表单

正文:ms-validate指令,只能用于form元素上,用于为表单添加验证功能。它需要与ms-duplex, ms-rules指令一起配合使用。

ms-validate的值应该对应一个对象,由于对象比较大,建议写在vm,像下面那样:

vm.validate = {
   onValidateAll: function(reasons){
     //返回一个数组,如果长度为零说明没有错
   },

    onError: avalon.noop,//针对单个表单元素(使用了ms-duplex的input, select)
    onSuccess: avalon.noop,//针对单个表单元素
    onComplete: avalon.noop,//针对单个表单元素
    onReset: avalon.noop,//针对单个表单元素
    validateInBlur: true, // {Boolean} true,在blur事件中进行验证,触发onSuccess, onError, onComplete回调
    validateInKeyup: true, // {Boolean} true,在keyup事件中进行验证,触发onSuccess, onError, onComplete回调
    validateAllInSubmit: true, // {Boolean} true,在submit事件中执行onValidateAll回调
    resetInFocus: true, // {Boolean} true,在focus事件中执行onReset回调,
    deduplicateInValidateAll: false // {Boolean} false,在validateAll回调中对reason数组根据元素节点进行去重
}

在一般情况下validateInBlur, validateInKeyup,validateAllInSubmit,resetInFocus都是默认的就行了。

onError,onSuccess,onComplete, onValidateAll的第一个参数都是reasons对象,this指向被验证的元素,reason里面有你需要的各种东西.

var reason = {
    element: elem,
    data: field.data,
    message: elem.getAttribute("data-" + ruleName + "-message") || elem.getAttribute("data-message") || hook.message,
    validateRule: ruleName,
    getMessage: getMessage
}
<body>
<script>
    var vm = avalon.define({
        $id: "test",
        action: '',
        name: '',
        add: function(e) {
            e.preventDefault()
            this.action = "add.php";
            this.validate.onManual();
        },
        update: function(){
            this.action = "update.php";
            this.validate.onManual();
        },
        validate: {
            validateAllInSubmit: false,
            onSuccess: function(reasons, event) {
                console.log('成功',reasons)
            },
            onError: function(reasons, event) {
                console.log('有验证没有通过')
            },
            onValidateAll: function(reasons) {
                var form = this
                if(reasons.length) {
                    // 表单有错误
                    console.log("有验证没有通过",reasons);
                    return false;
                } else {
                    // 验证成功
                    form.submit()
                }
            }

        }
    })
</script>


<div ms-controller="test">
    <form :validate="@validate" id="f1" :attr="{ action: @action }">
        <input type="text" placeholder="Insert your Name" :duplex="@name" :rules="{ required: true, number:true }" />
        <input type="submit" value="新建用户" :click="@add"/>
        <input type="submit" value="修改用户" :click="@update"/>
    </form>

</div>
</body>

下面是一个多个验证在同一个页面中的例子:

<!DOCTYPE html>
<html>

<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src='./dist/avalon.js'></script>
</head>

<body>
    <script>
        var vm = avalon.define({
            $id: "test",
            action: '',
            name: '',
            email: '',
            add: function(e) {
                e.preventDefault()
                this.action = "add.php";
                this.validate.onManual();
            },
            update: function(e) {
                e.preventDefault()
                this.action = "update.php";
                this.validate.onManual();
            },
            add2: function(e) {
                e.preventDefault()
                this.action = "add.php";
                this.validate2.onManual();
            },
            update2: function(e) {
                e.preventDefault()
                this.action = "update.php";
                this.validate2.onManual();
            },
            validate: {
                name: 'validate1',
                validateAllInSubmit: false,
                onSuccess: function(reasons, event) {
                    console.log('成功', reasons)
                },
                onError: function(reasons, event) {
                    console.log('第1个表单没有通过验证 ', reasons.map(function(e) {
                        return e.getMessage()
                    }).join(' '))
                },
                onValidateAll: function(reasons) {

                    var form = this
                    if (reasons.length) {
                        // 表单有错误
                        console.log("第1个表单没有通过验证 ", reasons.map(function(e) {
                            return e.getMessage()
                        }).join(' '));
                        return false;
                    } else {
                        // 验证成功
                        // console.log(form) //在chrome控制台console面板勾选preserve log
                        form.submit() //这里会发生跳转
                    }
                }
            },
            validate2: {
                name: 'validate2',
                validateAllInSubmit: false,
                onSuccess: function(reasons, event) {
                    console.log('成功', reasons)
                },
                onError: function(reasons, event) {
                    console.log('第二个表单没有通过验证!', reasons.map(function(e) {
                        return e.getMessage()
                    }).join(' '))
                },
                onValidateAll: function(reasons) {

                    var form = this
                    if (reasons.length) {
                        // 表单有错误
                        console.log("第二个表单没有通过验证!", reasons.map(function(e) {
                            return e.getMessage()
                        }).join(' '));
                        return false;
                    } else {
                        // 验证成功
                        //console.log(form) //在chrome控制台console面板勾选preserve log
                        form.submit() //这里会发生跳转
                    }
                }
            }
        })
    </script>

    <div ms-controller="test">
        <form :validate="@validate" id="f1" :attr="{ action: @action }">
            <input type="text" placeholder="input your name" :duplex="@name" :rules="{ required: true, number:true }" />
            <input type="submit" value="新建用户" :click="@add" />
            <input type="submit" value="修改用户" :click="@update" />
        </form>
        <form :validate="@validate2" id="f2" :attr="{ action: @action }">
            <input type="text" placeholder="input your email" :duplex="@email" :rules="{ required: true, email:true }" />
            <input type="submit" value="新建邮箱" :click="@add2" />
            <input type="submit" value="修改邮箱" :click="@update2" />
        </form>
    </div>
</body>

</html>
最后说一句,ms-validate配合ms-rules让你的表单跟完美,下一个我继续介绍配合ms-rules的使用。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Verilog代码,用于测试Avalon总线的读写操作: ``` module Avalon_Test ( input clk, input reset, input read_req, input [31:0] read_addr, output [31:0] read_data, input write_req, input [31:0] write_addr, input [31:0] write_data, input write_enable ); // Avalon bus signals reg [31:0] avalon_address; wire [31:0] avalon_read_data; reg [31:0] avalon_write_data; reg avalon_read_req; reg avalon_write_req; reg avalon_write_enable; // Test variables reg [31:0] test_data; reg test_write_enable; reg test_read_enable; reg [31:0] test_address; // Clock divider for slower data transfer reg clk_divider; // Counter for data verification reg [31:0] counter; // Avalon slave module instantiation Avalon_Slave dut ( .clk (clk), .reset (reset), .read_req (avalon_read_req), .read_addr (avalon_address), .read_data (avalon_read_data), .write_req (avalon_write_req), .write_addr (avalon_address), .write_data (avalon_write_data), .write_enable (avalon_write_enable) ); // Testbench initial begin // Initialize variables test_data = 0; test_write_enable = 0; test_read_enable = 0; test_address = 0; counter = 0; clk_divider = 0; // Reset the Avalon slave module reset = 1; #10; reset = 0; #10; // Write to the Avalon slave module test_data = 123; test_write_enable = 1; test_address = 4; #10; // Read from the Avalon slave module test_read_enable = 1; test_address = 4; #10; // Verify the read data if (test_data == avalon_read_data) begin $display("Data verification successful!"); end else begin $display("Data verification failed!"); end // End the simulation $finish; end // Clock divider always @(posedge clk) begin if (clk_divider == 1000) begin clk_divider <= 0; end else begin clk_divider <= clk_divider + 1; end end // Write data to the Avalon slave module always @(posedge clk) begin if (clk_divider == 0) begin avalon_address <= test_address; avalon_write_data <= test_data; avalon_write_req <= test_write_enable; avalon_write_enable <= write_enable; end end // Read data from the Avalon slave module always @(posedge clk) begin if (clk_divider == 0) begin avalon_address <= test_address; avalon_read_req <= test_read_enable; end end // Counter for data verification always @(posedge clk) begin if (clk_divider == 0) begin counter <= counter + 1; end end // Assign the read data to the output assign read_data = avalon_read_data; endmodule ``` 这个测试代码包括一个Avalon从设备(Avalon_Slave)模块和一个测试模块(Avalon_Test)。测试模块将从设备模块连接到Avalon总线,并通过写入和读取来测试总线操作。测试数据为123,写入地址为4,读取地址也为4。这个测试代码还包括一个计数器,用于验证读取的数据是否正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值