检查银条形式的验证错误


Check for validation Errors in Silverstripe Form

我有一个显示为模态的表单。仅当用户单击相应的链接时,才会显示此模式。表单本身按预期工作。

我只遇到一个主要问题。每当表单无效时,必填字段验证器就会启动,用户就会被引导回去,仅此而已。

在这种情况下,我想立即显示模态,但我找不到检查验证错误的方法。

是否有遇到

相同问题的人设法解决了它,或者是否有人可以帮助我解决这个问题?

谢谢!

我前段时间做的一个 Boostrap 网站有以下解决方案。我没有替换整个页面,而只替换了模态:

jQuery(function($) {
$('form[data-async]').on('submit', function(event) {
var $form = $(this);
var $target = $($form.attr('data-target'));
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
success: function(data, status) {
$target.html(data);
}
});
event.preventDefault();
});
}); 

这会将内容替换为实际响应。 在这里找到它

在模板中:

<!-- Bootstrap trigger to open modal -->
<a data-toggle="modal" href="#rating-modal">Write a Review</a>
<div class="hide fade modal" id="rating-modal">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h2>Your Review</h2>
</div>
<div class="modal-body">
<!-- The async form to send and replace the modals content with its response -->
<form class="form-horizontal well" data-async data-target="#rating-modal" action="/some-endpoint" method="POST">
<fieldset>
<!-- form content -->
</fieldset>
</form>
</div>
<div class="modal-footer">
<a href="#" class="btn" data-dismiss="modal">Cancel</a>
</div>
</div> 

请务必使用 Director::is_ajax() 签入您的 php 代码,如果您有 ajax 请求或不使用正确的模板进行响应。