【问题标题】:How to add validation to fields in Odoo?如何向 Odoo 中的字段添加验证?
【发布时间】:2018-05-26 17:35:47
【问题描述】:

我必须在 Odoo 的注册表单中添加验证(这是 auth_signup 模块中的 auth_signup_login_templates.xml 文件。我需要确保 Name 包含字母并且在 3 到 15 个字符之内。现在,默认情况下,名称的代码是:

         <div class="form-group field-name">
            <label for="name" class="control-label">Your Name</label>
            <input type="text" name="name" t-att-value="name" id="name" class="form-control" placeholder="e.g. John Doe"
                required="required" t-att-readonly="'readonly' if only_passwords else None"
                t-att-autofocus="'autofocus' if login and not only_passwords else None" />
        </div>

xml页面可以找到here

【问题讨论】:

    标签: xml validation view field odoo


    【解决方案1】:

    对于验证,我们更喜欢 Javascript/Jquery :) 例如:

    $('#name').keypress(function (e) {
        var regex = new RegExp(/^[a-zA-Z\s]+$/);
        var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
        if (regex.test(str)) {
            return true;
        }
        else {
            e.preventDefault();
            return false;
        }
    });
    

    但是对于最小和最大长度,您可以使用数据属性。 例如:

    <input data-rule-minlength="3" data-rule-maxlength="8" data-msg-minlength="Exactly 3 characters please" data-msg-maxlength="Exactly 8 characters please">
    

    【讨论】:

    • 哦。好的。我使用的 html 模式也很好用,并且需要更少的代码行。但是,这似乎也很好。谢谢。
    • @MinggLex 好的,那么您可以接受并支持我的回答,这也会对其他人有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多