简体   繁体   English

AngularJS中未显示必需的错误消息

[英]Required error messages not getting displayed in AngularJS

I am having trouble with displaying required error messages in one of my pages. 我在其中一页中显示必需的错误消息时遇到麻烦。

HTML : HTML:

<div ng-form="editReservationForm">
    <form id="edit-profile" novalidate name="editReservationForm" autocomplete="off" class="form-horizontal">
        <fieldset>
            <div class="control-group">
                <label class="control-label" for="reservation.employee.firstName">First Name<sup>*</sup></label>
                <div class="controls">
                    <input class="span4" name="reservation.employee.firstName" placeholder="First Name" ng-model="reservation.employee.firstName" disabled />
                </div> <!-- /controls -->
            </div> <!-- /control-group -->


            <div class="control-group">
                <label class="control-label" for="reservation.employee.lastName">Last Name<sup>*</sup></label>
                <div class="controls">
                    <input class="span4" name="reservation.employee.lastName" placeholder="Last Name" ng-model="reservation.employee.lastName" disabled />
                </div> <!-- /controls -->
            </div> 

            <div class="control-group">
                <label class="control-label" for="reservation.reservedFrom">Reserved From<sup>*</sup></label>
                <div class="controls input-group date" data-provide="datepicker">
                    <input class="form-control" type="text" name="startDate" core-date-picker ng-model="reservation.reservedFrom" required>
                    <div class="input-group-addon">
                        <span class="glyphicon glyphicon-th"></span>
                    </div>

                    <span style="color:red">{{errMessageFrom}}</span>

                </div> <!-- /controls -->
            </div> <!-- /control-group -->
            <div class="control-group">
                <label class="control-label" for="reservation.reservedTill">Reserved Till<sup>*</sup></label>
                <div class="controls input-group date" data-provide="datepicker">
                    <!--<input type="text" style="width:150px" class="span4" name="reservedTill" placeholder="Reserved Till" data-ng-model="reservation.reservedTill"
                           validator="required" required-error-message="Date is required" valid-method="watch" id="endDate" ng-change='checkErr()' />-->
                    <input class="form-control" type="text" name="EndDate" core-date-picker ng-model="reservation.reservedTill"  ng-change='checkErr()' id="endDate1" required>
                    <div class="input-group-addon">
                        <span class="glyphicon glyphicon-th"></span>
                    </div>
                    <span  style="color:red">{{errMessageTo}}</span>

                </div> <!-- /controls -->
            </div> <!-- /control-group -->



            <!--DATE PICKER-->
            <div class="control-group">
                <label class="control-label" for="reservation.account.name" >Account Name<sup>*</sup></label>
                <div class="controls">
                    <select ng-model="reservation.account.id" required>
                        <option ng-repeat="p in accounts"  value="{{p.id}}" required>{{p.name}}</option>
                    </select>
                </div> <!-- /controls -->
            </div> <!-- /control-group -->

            <!--<div class="control-group">
                <label class="control-label" for="reservation.poc.name">POC Name</label>
                <div class="controls">
                    <select ng-model="reservation.poc.id">
                        <option ng-repeat="p in pocs" value="{{p.id}}">{{p.name}}</option>
                    </select>
                </div> <!-- /controls -->
            <!--</div>--> <!-- /control-group -->
            <div class="control-group">
                <label class="control-label" for="reservation.remark" >Remarks : <sup>*</sup></label>
                <div class="controls">
                    <input type="text" required id="remarksText" class="span4" name="reservation.remark" placeholder="Enter your remarks here" ng-model="reservation.remark" />
                </div> <!-- /controls -->
            </div> <!-- /control-group -->

            <div class="form-actions">
                <button type="button" class="btn btn-primary" validation-submit="editReservationForm" ng-click="updateReservation()">Save</button>
                <button ng-click="cancel()" class="btn">Cancel</button>
            </div> <!-- /form-actions -->
        </fieldset>
    </form>

</div>

I have added the required tag along with all input fields but upon click of 'Save' button, the error messages that show 'This field is required' is not shown. 我已经在所有输入字段中添加了required标签,但是单击“保存”按钮后,不显示“此字段为必填”的错误消息。 The function is not getting called though. 该函数没有被调用。

I need help in finding the cause of this issue. 我需要帮助找到此问题的原因。

Keep the novalidate in the <form> tag. novalidate保留在<form>标记中。

Try changing required to ng-required="true" . 尝试将required更改为ng-required="true"

ng-required works with ng-model . ng-required使用ng-model作品。 So make sure that ng-model exists in your input tag. 因此,请确保您的输入标签中存在ng-model。 (additional info) (附加信息)

Try adding 尝试添加

<span style="color:red" ng-show="editReservationForm.inputFieldName.$invalid && editReservationForm.inputFieldName.$touched">
                Required field.
</span>

where inputFieldName is the respective name attribute. 其中inputFieldName是各自的name属性。

This adds for better effect although you'll need to import angular-animate.min.js and add it as a dependency too. 尽管您需要导入angular-animate.min.js并将其也添加为依赖项,但这可以增加效果。

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

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