GithubHelp home page GithubHelp logo

Comments (3)

olditer avatar olditer commented on July 19, 2024 1

根据贤大的回复,我选择了一种做法思路

1、提供一个函数,用来包装既有的verify,这样原来的verify机制不用打破

        // 验证器包装
        var OLD_VERIFY = {};
        var NEW_VERIFY = {};
        // 包装验证规则
        var decorateVerify = function() {
            var hasChange = false;
            layui.each(form.config.verify, function (_key, _fn) {
                if (NEW_VERIFY[_key]) {
                    return;
                }
                hasChange = true;
                // 保存旧定义
                OLD_VERIFY[_key] = form.config.verify[_key];
                // 重新定义
                NEW_VERIFY[_key] = function(){
                    var fnKey = _key;
                    return function(value, item) {
                        // 调用旧的
                        var result = OLD_VERIFY[fnKey](value, item);
                        $(item).parent().find(".eip-form-error").remove();
                        if (result) {
                            result = $(item).attr('lay-msg') || result;
                            $(item).parent().append('<span class="eip-form-error">' + result + '</span>');
                            // 阻止系统弹框
                            return true;
                        }
                        return false;
                    };
                }();
            });
            if (hasChange) {
                form.verify(NEW_VERIFY);
            }
            return hasChange;
        };

2、重载submit函数和事件

        // 提交
        var _submit = form.constructor.prototype.submit;
        var submit = form.constructor.prototype.submit = function(filter, fnCallback) {
            // 重新包装下Verify
            fnDecorateVerify();
            var $form = form.getFormElem(filter);
            $form.find('*[lay-verify]').each(function(_1, _item){
                form.validate.call(form, _item);
            });
            _submit.apply(this, arguments);
        };
        // 重新绑定事件
        $(document).off('submit', '.layui-form', _submit)
            .off('click', '*[lay-submit]', _submit)
            .on('submit', '.layui-form', submit)
            .on('click', '*[lay-submit]', submit);

如此一来,不需要大家修改既有的代码逻辑。
可以根据自己的实际情况修改 decorateVerify函数的逻辑

请大家批评指正

from layui.

sentsim avatar sentsim commented on July 19, 2024

问题 1. lay-reqtext 属性改成 lay-msg 即可,与下面 required 函数中的 text 变量处的 lay-msg 保持一致。
问题 2. 你的 HTML 示例中 <button type="button" class="layui-btn" lay-filter="demo1" id="LAY-submit">立即提交</button> 缺少 lay-submit 属性,导致内置的顺序校验失效,因此焦点无法从第一个开始。

优化后的示例:https://codepen.io/layui/pen/ExBayJg?editors=1010

from layui.

olditer avatar olditer commented on July 19, 2024

感谢回答。 不过,问题2的解决方式貌似是进行了2遍校验
第一遍是自己写的循环调用form.validate,第二遍是系统默认submit处理逻辑(只检查第一个)
因为我看到form.js中注册了事件

  // 表单提交事件
  $dom.on('submit', ELEM, submit)
  .on('click', '*[lay-submit]', submit);

所以最终其实是第二次验证的设定焦点起了效果,虽然能解决问题,但是检查2次有些奇怪

from layui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.