简体   繁体   English

ASP.NET,MVC4,Unobtrusive Validation - 在bootstrap手风琴中将错误摘要显示为叠加

[英]ASP.NET, MVC4 , Unobtrusive Validation - Show error-summary as overlay in bootstrap accordion

I have a form where content is dynamically added as new accordion-part which can be collapsed and expanded. 我有一个表单,其中内容被动态添加为新的accordion-part,可以折叠和展开。 The added content has the same model behind which is validated correct. 添加的内容具有相同的模型,其后面的验证是正确的。

What I want is to have some kind of error-indicator on the header (where the added part can be collapsed/expanded), showing the errors of the inside-part as overlay. 我想要的是在标题上有一些错误指示符(添加的部分可以折叠/展开),显示内部部分的错误作为叠加。 But this only when the part is collapsed (so the user knows that there are errors inside and he has to expand the content). 但这只是在部件折叠时(因此用户知道内部存在错误并且他必须扩展内容)。

In case it is expanded, the ValidationMessageFor() helper is doing fine. 如果它被扩展, ValidationMessageFor()帮助程序正常。

My idea here: 我的想法在这里:

  • bind to some event to know when the form is invalid 绑定到某个事件以了解表单何时无效
  • check if there are errors inside an collapsed element 检查折叠元素中是否存在错误
  • if so, add icon with mouseover-overlay to the header of this content 如果是这样,请将带有鼠标悬停覆盖的图标添加到此内容的标题中

The quesion: 问题:

  • How can this be achieved? 怎么能实现这一目标?

Update #1 更新#1

The header looks like this: 标题看起来像这样:

    <a data-toggle="collapse" data-parent="#tourStops" href="#@(eventId)Content" aria-expanded="true" aria-controls="@(eventId)Content">
        <div class="panel-heading btn-heading" role="tab" id="heading@(eventId)">
            <span class="pull-left" id="eventName@(eventId)">Event name</span>
            <button class="btn btn-danger btn-sm" data-tourstop-id="@eventId" id="RemoveTourStopButton" style="z-index: 100;">@Translator.TranslateTour(Keys.Tour.CreateRemoveTourStop)</button>
        </div>
    </a>

I want to have the icon next to the <span> which contains the title showing the summarized errors on mouseover and only if the content below is collapsed. 我希望在<span>旁边有一个图标,其中包含显示鼠标悬停时汇总错误的标题,并且仅在下面的内容折叠时才显示。

The content is something like: 内容如下:

<div id="@(eventId)Content" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading@(eventId)">
    <div class="panel-body">
        Awesome content here...
    </div>
</div>

If the container with id "@(eventId)Content" has the class in it is expanded, so I could check for non-existence of this class to determine if the content is collapsed. 如果ID为“@(事件ID)内容的”容器具有类in它被扩大,所以我可以检查这个类的不存在,以确定该内容已展开。

But still I have no idea how to hook on the the form contains errors-event to make further changes to the DOM depeding on where those errors are and if the container is collapsed/expanded. 但是我仍然不知道如何挂钩the form contains errors-event以对DOM进行进一步的更改,这取决于这些错误的位置以及容器是否已折叠/展开。

Update #2 更新#2

The jQuery-part looks like: jQuery部分看起来像:

form.validate().settings.ignore = ":disabled";
form.submit();

How about using ValidationSummary() in the header? 如何在标题中使用ValidationSummary()

The ValidationSummary method displays a list of all validation messages on the page. ValidationSummary方法显示页面上所有验证消息的列表。

As to the updated code in the question: I don't have any way to try this at the moment, so this is all example code that might help you get to forward. 至于问题中的更新代码:我目前没有任何方法可以尝试这一点,所以这是可以帮助您前进的所有示例代码。

You can check the entire form for errors by class (but I'm not sure at the time of writing what the class name for the errors is), and traverse to the corresponding header: 您可以按类检查整个表单的错误(但我在编写错误的类名时不确定),并遍历到相应的标题:

function checkForErrors() {
   $('form').find(".field-validation-error").each(function() {
     var panelElement = $(this).closest('.panel-collapse');
     if (!panelElement.hasClass('in')) {
       var idOfHeader = panelElement.attr('aria-labelledby');
       $('#'+idOfHeader).find('.customErrorClass').html('<span class="glyphicon glyphicon-exclamation-sign" title="This section contains errors"></span> ').show();
     }
   });
}

As for the validation, place a call to checkForErrors() after the validation has failed. 至于验证,在验证失败后调用checkForErrors()。 One way to do that might be: 一种方法可能是:

$.validator.unobtrusive.parse($('form'));
if (!$('form').valid()) {
    checkForErrors();
}

Or 要么

$('form').bind('invalid-form.validate', function (form, validator) {
   checkForErrors();
});

I tested it quickly in Bootply ( http://www.bootply.com/JqVsSaXnHM ). 我在Bootply中快速测试了它( http://www.bootply.com/JqVsSaXnHM )。 The first accordian has no fake error, the second has an error. 第一个手风琴没有假错误,第二个有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM