简体   繁体   English

角度模糊验证阻止提交单独的表单

[英]Angular blur validation preventing submission of separate form

I have an Angular page with a form for adding people, and a button (outside this form) to submit the list of people. 我有一个Angular页面,其中包含用于添加人员的表单,以及一个用于提交人员列表的按钮(在此表单之外)。

When the user focuses on a text input in the form, and then clicks to submit the list, the validation error from the text input appears but the submit event never occurs. 当用户关注表单中的文本输入,然后单击以提交列表时,将显示文本输入中的验证错误,但不会发生提交事件。

An example of this issue here: http://plnkr.co/edit/6Z0UUs 这个问题的一个例子是: http//plnkr.co/edit/6Z0UUs

<div ng-controller="MyCtrl as vm">
  <form name="form1" novalidate="">
    <input type="text" name="field1" ng-model="vm.name" required>
    <div ng-messages="form1.field1.$error" ng-if="form1.field1.$touched">
      <label ng-message="required">Name is required</label>
    </div>
    <!-- 
    This form adds a person to a list. I've not included this code to keep the example simple 
    <input type="submit" value="Add person"> 
    -->
  </form>
  <button ng-click="vm.submit()">Submit list</button>
</div>

- -

angular.module('myApp',[])
.controller('MyCtrl', function() {
    var vm = this;

    vm.name = '';

    vm.submit = function () {
        alert('submitted');    
    };
});

To replicate: 复制:

  1. Click on the text input but leave blank 单击文本输入但保留空白
  2. Click submit 点击提交

Current behavior: "Name is required" appears thanks to the validation. 当前行为:由于验证,出现“需要名称”。 Clicking 'Submit' again displays the 'submitted' alert. 再次单击“提交”将显示“已提交”警报。

Expected behavior: "Name is required" appears thanks to the validation and the 'submitted' alert appears. 预期行为:由于验证出现“需要名称”, 显示“已提交”警报。

Desired behavior: The 'submitted' alert appears and I add some code to vm.submit() to hide the 'Name is required' validation message as it's not important when the list is submitted. 期望的行为:出现“已提交”警报,我向vm.submit()添加一些代码以隐藏“名称是必需的”验证消息,因为它在提交列表时并不重要。

I've observed that removing the ng-messages block fixes the issue, but I do need to show a validation message. 我观察到删除ng-messages块可以解决问题,但我确实需要显示验证消息。 Using a more basic directive ( ng-show ) to show the validation message instead does not help. 使用更基本的指令( ng-show )来显示验证消息并没有帮助。

Any insights into what I'm doing wrong, or workarounds to achieve my desired behavior, would be great! 任何关于我做错了什么的见解,或者通过变通方法来实现我想要的行为,都会很棒!

[Modified Answer] : Here is a working plunker : http://plnkr.co/edit/JCyRi8xp4L3FtafABblS?p=preview [修改后的答案]:这是一个工作的插件: http ://plnkr.co/edit/JCyRi8xp4L3FtafABblS?p =preview

vm.preventDefaultIfSubmitted = function($event){
if($event.relatedTarget.id == "submit"){
    $event.stopImmediatePropagation();
  }
};

The idea is to get the $event when the blur occurs and then to look at the id of the relatedTarget (which is your button in this case) and if it is then you cancel the $event, otherwise you keep it. 想法是在模糊发生时获取$事件,然后查看relatedTarget的id(在这种情况下是你的按钮),如果是,则取消$ event,否则你保留它。

That way if you click anywhere your validation message appears and if you click on submit it doesnt 这样,如果您点击验证消息出现的任何地方,如果您点击提交它不会

May this help for form validation 可以帮助进行表单验证

<form role="form" name="projectBriefFrm">
                    <div class="form-group">
                        <label for="project_title">Project Title
                            <sup>*</sup>
                        </label>
                        <input ng-model="project.title" ng-required="true" type="text" class="form-control" name="title" placeholder="Untitled Project"
                        />
                        <div class="row">
                            <span class="errorMessage" ng-show="!isProjectModelValid && projectBriefFrm.title.$invalid">Please provide title</span>
                        </div>
                        <div class="container" id="editor-title">
                            <label for="project_brief">Project Brief
                                <sup>*</sup>
                            </label>
                            <div class="row" id="editor">
                                <div class="col-lg-12 nopadding">
                                    <textarea ng-model="project.brief" name="brief" cult-editor ng-required="true"></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <span class="errorMessage" ng-show="!isProjectModelValid && projectBriefFrm.brief.$invalid">Please provide project brief</span>
                        </div>
                    </div>
                </form>

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

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