0

在表单中有选择框,我尝试进行验证,但与我之前对输入类型文本所做的工作不同。我做错了什么?

例子

<div class="form-group">
                                <label class="control-label col-sm-3">Email<sup>*</sup></label>

                                        <div class="col-sm-6">

                                            <div class="input-icon-right">
                                                <i class="fa fa-envelope"></i>
                                                <input class="form-control" placeholder="example@example.com" type="email" ng-model="patient.email" name="email" required>
                                                <p ng-show="myForm.email.$error.required" style="color:red" ng-if="myForm.email.$touched">Add your email</p>

                                            </div>

                                        </div>
                            </div>

此代码运行良好,并在字段为空时显示错误消息。

并且这个带有选择框的代码不能正常工作,每次选择没有空但有一个选择-选择-,当没有另一个选择时我如何显示错误。

<div class="form-group">
                                <label class="col-sm-3 control-label">{{getWord('Gender')}}<sup>*</sup></label>
                                <div class="col-sm-6">
                                    <select ui-select2 ng-model="patient.gender"
                                            data-placeholder="Tên Cơ Sở"
                                            ng-change="changeGender()"
                                            style="width: 100%;" requied name="gender">
                                        <option value="">-{{getWord('Select')}}-</option>
                                        <option ng-repeat="item in genders" value="{{item.name}}">
                                            {{getWord(item.name)}}
                                        </option>
                                    </select>
                                    <p ng-show="myForm.gender.$error.required" style="color:red" ng-if="myForm.gender.$touched">Please choice gender</p>
                                </div>
                            </div>
4

3 回答 3

0
<div class="form-group">
                            <label class="col-sm-3 control-label">{{getWord('Gender')}}<sup>*</sup></label>
                            <div class="col-sm-6">
                                <select ui-select2 ng-model="patient.gender"
                                        data-placeholder="Tên Cơ Sở"
                                        ng-change="changeGender()"
                                        style="width: 100%;" required name="gender">
                                    <option value="" disabled selected>-{{getWord('Select')}}-</option>
                                    <option ng-repeat="item in genders" value="{{item.name}}">
                                        {{getWord(item.name)}}
                                    </option>
                                </select>
                                <p style="color:red" ng-if="!myForm.gender.$pristine && myForm.gender.$error.required">Please choice gender</p>
                            </div>
                        </div>

你能试试这个吗?

于 2016-07-28T09:51:42.173 回答
0

据我了解。当用户未选择任何内容或未触摸下拉菜单时,您需要显示错误。

更改此行:

<p ng-show="myForm.gender.$error.required" style="color:red" ng-if="myForm.gender.$touched">Please choice gender</p>

对此:

<p style="color:red" ng-if="myForm.gender.$pristine || myForm.gender.$error.required">Please choice gender</p>

而且还有一个拼写错误。更改requiedrequired

也更正一下:

<select ui-select2 ng-model="patient.gender"
        data-placeholder="Tên Cơ Sở"
        ng-change="changeGender()"
        style="width: 100%;" requied 
name="gender">

对此:

<select ui-select2 ng-model="patient.gender"
        ng-change="changeGender()"
        style="width: 100%;" required 
name="gender">

希望这会有所帮助。

于 2016-07-28T12:28:25.987 回答
0

我的最终代码。但不是我想要的好

<div class="form-group" ng-class="{ 'has-error': myForm.gender.$pristine && myForm.gender.$invalid }">
                                <label class="col-sm-3 control-label">{{getWord('Gender')}}<sup>*</sup></label>
                                <div class="col-sm-6">
                                    <select ui-select2 ng-model="patient.gender" data-placeholder="Tên Cơ Sở" ng-change="changeGender(filterType)" style="width: 100%;" required name="gender">
                                        <option value="">-{{getWord('Select')}}-</option>
                                        <option ng-repeat="item in genders" value="{{item.name}}" >
                                            {{getWord(item.name)}}
                                        </option>
                                    </select>

                                     <p style="color:red" ng-if="myForm.gender.$pristine || myForm.gender.$error.required">Please choice gender</p>
                                </div>
                            </div>

截屏

于 2016-07-28T15:42:36.433 回答