如果形式数据中存在错误,请使用AJAX停止加载后续模式
我的网站上有一个由一系列模式组成的多步格式。当我单击下一步时,我触发下一个模式并提交表单数据。我试图在这些模式上设置验证,在该模态上,我要么停止序列中的下一个模态的激活,要么在缺少数据时重新加载当前模式。在我的ajax调用中,我可以显示存在错误,但是当我使用$('#modal_2_toggle')时。modal('show');
尝试重新加载发生错误发生的模式,我的代码仍将用户传递到序列中的下一个模式。我正在寻找阻止下一个模式的加载,直到data.error
为false。这是我的html的html:
<div class="modal fade" id="Modal_2_Toggle" aria-hidden="true" aria-labelledby="Modal_2_ToggleLabel" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="Modal_2_ToggleLabel">Frage 2</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: 18%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">18%</div>
</div>
<form method="POST" enctype="multipart/form-data", id="form_2">
{{ form_2.csrf_token }}
<p>
<br>
{{ form_2.consumption.label(class="form-label") }}
{{ form_2.consumption(class="form-control form-control-sm", id="consumptionInput") }}
</p>
{{ form_2.submit(class="btn btn-primary", id="form_2_submit", hidden="true") }}
<br>
</form>
<div id="errorAlert" class="alert alert-danger" role="alert" style="display:none;"></div>
</div>
<div class="modal-footer">
<button class="btn btn-primary modal_btn" data-bs-target="#Modal_1_Toggle" data-bs-toggle="modal" data-bs-dismiss="modal">Vorherig</button>
<button class="btn btn-primary modal_btn" data-bs-target="#Modal_3_Toggle" data-bs-toggle="modal" data-bs-dismiss="modal" form="form_2" id="modal_2_confirm">Weiter</button>
</div>
</div>
</div>
</div>
这是我的JS:
$(document).ready(function() {
$('#form_2').on('submit', function(event) {
$.ajax({
data : {
consumption : $('#consumptionInput').val(),
},
type : 'POST',
url : '/test'
})
.done(function(data){
if (data.error) {
$('#errorAlert').text(data.error).show();
$('#Modal_2_Toggle').modal('show'); <- I ASSUMED THIS WOULD RELOAD THE MODAL
}
});
event.preventDefault();
});
});
I have a multi-step form on my website which is made up of a series of modals. When I click next, I trigger the next modal and submit form data. I am trying to set up validation on these modals, where I either stop the activation of the next modal in the sequence, or reload the current modal if there is missing data. In my ajax call I can show that an error is present, but when I use $('#Modal_2_Toggle').modal('show');
to try to reload the modal where the error took place, my code still passes the user to the next modal in the sequence. I am looking for help with blocking the loading of the next modal until data.error
is false. Here is my HTML for the modal in question:
<div class="modal fade" id="Modal_2_Toggle" aria-hidden="true" aria-labelledby="Modal_2_ToggleLabel" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="Modal_2_ToggleLabel">Frage 2</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: 18%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">18%</div>
</div>
<form method="POST" enctype="multipart/form-data", id="form_2">
{{ form_2.csrf_token }}
<p>
<br>
{{ form_2.consumption.label(class="form-label") }}
{{ form_2.consumption(class="form-control form-control-sm", id="consumptionInput") }}
</p>
{{ form_2.submit(class="btn btn-primary", id="form_2_submit", hidden="true") }}
<br>
</form>
<div id="errorAlert" class="alert alert-danger" role="alert" style="display:none;"></div>
</div>
<div class="modal-footer">
<button class="btn btn-primary modal_btn" data-bs-target="#Modal_1_Toggle" data-bs-toggle="modal" data-bs-dismiss="modal">Vorherig</button>
<button class="btn btn-primary modal_btn" data-bs-target="#Modal_3_Toggle" data-bs-toggle="modal" data-bs-dismiss="modal" form="form_2" id="modal_2_confirm">Weiter</button>
</div>
</div>
</div>
</div>
Here is my JS:
$(document).ready(function() {
$('#form_2').on('submit', function(event) {
$.ajax({
data : {
consumption : $('#consumptionInput').val(),
},
type : 'POST',
url : '/test'
})
.done(function(data){
if (data.error) {
$('#errorAlert').text(data.error).show();
$('#Modal_2_Toggle').modal('show'); <- I ASSUMED THIS WOULD RELOAD THE MODAL
}
});
event.preventDefault();
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您的多步骤形式的方法是否可符合您的需求。
也许您将使用当前的JavaScript框架之一,例如Angular,React,Vue或Svelte,更容易地实现目标。
然而,以下示例使用了带有集成幻灯片的单个模态对话框,该对话可以被激活并互相停用。它解决了您的问题,即下一步直到上一个步骤正确完成。服务器返回的错误代码确定提到的行为。
I'm not sure if your multi-step form approach is viable with your needs.
Perhaps you would reach your goal more easily and directly with one of the current JavaScript frameworks such as Angular, React, Vue or Svelte.
Nevertheless, the following example uses a single modal dialog with integrated slides, which can be activated and deactivated one after the other. It solves your problem that the next step cannot be called until the previous one has been completed correctly. The error code returned by the server determines the behavior mentioned.