【问题标题】:Check for validation Errors in Silverstripe Form检查 Silverstripe 表单中的验证错误
【发布时间】:2015-01-12 09:37:35
【问题描述】:

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

我遇到的主要问题只有一个。每当表单无效时,RequiredFields Validator 就会启动,用户会被引导回来,仅此而已。

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

是否有人遇到同样的问题并设法解决了这个问题,或者有人可以帮助我解决这个问题吗?

谢谢!

【问题讨论】:

    标签: php validation silverstripe


    【解决方案1】:

    我对我前段时间做的一个 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();
    });
    }); 
    

    这会将内容替换为实际响应。找到了here

    在模板中:

    <!-- 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> 
    

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

    【讨论】:

    • 感谢您的快速回复。我确实喜欢异步方法。我会试一试,如果它适合我​​,我会回来的。
    • 我也在使用 RecaptchaFields。事实证明,这种异步方法比我想象的要复杂。提交表单后无法启动后端验证。
    • 我更喜欢 addons.silverstripe.org/add-ons/camspiers/honeypot 蜜罐字段来保护垃圾邮件。也许这对你来说也足够了
    猜你喜欢
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 2016-02-15
    • 1970-01-01
    相关资源
    最近更新 更多