简体   繁体   English

AngularJS中的表单验证和动态控件

[英]Form validation and dynamic controls in AngularJS

I have a form with a section of controls. 我有一个带有控件部分的表单。 I have a button which adds more sections on the form dynamically. 我有一个按钮,可以动态地在表单上添加更多部分。

I have validations on each of those controls ie ng-required , and other validations. 我对每个控件都进行了验证,即ng-required和其他验证。

I have a submit button and I have a validation error on click. 我有一个提交button ,我在点击时遇到验证错误。

I have invalid values in the control and click submit, the validations are fired and all is good. 我在控件中有无效值并单击提交,验证被触发,一切都很好。 After clicking the submit button if I try to add a new section, because the section has controls with empty values and since the form is submitted the validations fire and by default I see error messages, which is not a good user experience, hence can someone please help me to avoid this behaviour? 如果我尝试添加新的部分,单击提交按钮后,因为该部分具有空值的控件,并且由于表单被提交,验证会激活,默认情况下我会看到错误消息,这不是一个好的用户体验,因此可以有人请帮我避免这种行为?

Please find below the code I have: 请在下面找到我的代码:

<div class="row bottom-spaced-large">
        <div class="row">
            <div class="col-md-3">
                Number of ear tags
            </div>
            <div class="col-md-9">
                <input name="txtNumber" tabindex="102" ng-pattern="/^[0-9]*$/" ng-maxlength="50" ng-model="someValue" class="form-control color-black" tabindex="3" />
                <div class="row" style="padding-left: 30px" ng-if="someForm.$submitted && !someForm.txtNumber.$valid">
                    <span style="color: red">Number must be a positive number.</span>
                </div>
            </div>
        </div>
    </div>


<div class="row bottom-spaced-large">
        <button type="button" class="btn btn-primary btn-add" tabindex="101" ng-click="addMore();">Add more<span aria-hidden="true" class="fa fa-plus"></span></button>
    </div>


<button type="submit" class="btn btn-default btn-xs fixed-width" ng-click="doSomething()" tabindex="104">Next</button>

As per the standard practices, you should disable the save button until the form is valid and show the validation messages only when user interacts with the field (ie when the field is touched). 根据标准做法,您应禁用保存按钮,直到表单有效,并仅在用户与字段交互时显示验证消息(即触摸字段时)。

For this angular provides $touched attribute on form field which is set true only when user interacts with the field. 对于此角度,在表单字段上提供$touched属性,仅当用户与字段交互时才设置为true。

So the following code will solve your problem 所以下面的代码将解决您的问题

<input name="txtNumber" tabindex="102" ng-pattern="/^[0-9]*$/" ng-maxlength="50" ng-model="someValue" class="form-control color-black" tabindex="3" />
                <div class="row" style="padding-left: 30px" ng-if="someForm.$submitted && !someForm.txtNumber.$valid">

<div class="row" style="padding-left: 30px" ng-show="someForm.txtNumber.$touched && someForm.txtNumber.$invalid">
  <span style="color: red">Number must be a positive number.</span>
</div>

<button type="submit" ng-disabled="someForm.$invalid" class="btn btn-default btn-xs fixed-width" ng-click="doSomething()" tabindex="104">Next</button>

Edit: If you want to prompt users why save button is disabled. 编辑:如果要提示用户禁用保存按钮的原因。 You can put the following div at the top of the form. 您可以将以下div放在表单的顶部。

<div ng-show="someForm.$invalid && someForm.$pristine" class="bg-danger">  
   Please fill the form to continue!
</div>

$pristine is set true if no changes are made to the value of form field (even if field is touched and no changes are made it is true), Whereas $touched is set true on touching the field no matter value is changed or not. 如果没有对表单字段的值进行任何更改,则$pristine设置为true(即使触摸了字段且未进行任何更改也是如此),而触摸字段时,无论值是否更改, $touched都设置为true。

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

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