【问题标题】:Bootstrap validation not working despite including script and following docs尽管包含脚本和以下文档,但引导验证不起作用
【发布时间】:2021-11-10 02:55:21
【问题描述】:

尽管页面和样板布局中都有脚本,但我无法验证表单。我尝试将标签移动到不同的地方,但服务器端验证是唯一有效的部分。表单不会运行客户端验证。

<% layout('layouts/boilerplate') %> 
<div class="row">
    <h1 class="text-center">Create Artist Profile</h1>
    <div class="col-6 offset-3">
        <form action="/artists/new" method="POST" novalidate class="validated-form" enctype="multipart/form-data">
            <div class="mb-3">
                <label class="form-label" for="email">Email</label>
                <input class="form-control" type="email" id="email" name="email" placeholder="name@domain.com" required>
                <div class="valid-feedback">
                    Looks good!
                </div>
            </div>
            <div class="mb-3">
                <label for="formFile" class="form-label">Upload band photo</label>
                <input class="form-control" type="file" id="image" name="image" multiple="false">
              </div>
                <div class="mb-3">
                    <button class="btn btn-success">Create Profile</button>
                </div>
            <a href="/artists">All artists</a>
        </form>        
    </div>
</div>

这是我的 layouts/boilerplate.ejs 正文中的脚本标签

    <script>
        (function () {
        'use strict'

        bsCustomFileInput.init()

        // Fetch all the forms we want to apply custom Bootstrap validation styles to
        const forms = document.querySelectorAll('.validated-form')

        // Loop over them and prevent submission
        Array.from(forms)
            .forEach(function (form) {
                form.addEventListener('submit', function (event) {
                    if (!form.checkValidity()) {
                        event.preventDefault()
                        event.stopPropagation()
                    }

                    form.classList.add('was-validated')
                }, false)
            })
    })
    </script>

任何想法我在此表单中做错了什么?

【问题讨论】:

    标签: javascript node.js forms mongoose


    【解决方案1】:

    您是否尝试过将表单类更改为“needs-validation”?

    也试试这个代码

       (function() {
        'use strict';
        window.addEventListener('load', function() {
            
    var forms = document.querySelectorAll('.validated-form')
           
            var validation = Array.prototype.filter.call(forms, function(form) {
                form.addEventListener('submit', function(event) {
                    if (form.checkValidity() === false) {
                        event.preventDefault();
                        event.stopPropagation();
                    }
                    form.classList.add('was-validated');
                }, false);
            });
        }, false);
    })();
    

    【讨论】:

    • 不走运(该字段是可编辑的,只要它与我的“类”字段以它应该可以工作的形式匹配)。表单验证在我第一次设置时可以工作,但我一定是在此过程中不小心编辑了一些东西,现在它不再工作了。
    • 我觉得问题出在Array.from(forms) .forEach(function (form) { form.addEventListener('submit', function (event) { if (!form.checkValidity()) {你声明数组和foreach的方式可以吗?
    • 我更新了代码,试试看是否有效
    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 嗨@nexdanger - 仍然没有运气。我很感激你尝试。我能够让我的服务器端验证工作(使用 Joi)所以我想知道在创建服务器端验证时,我是否以某种方式关闭了客户端验证。这可能是一个比我在上面的上下文中讨论的更大的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多