简单的角度形式验证不起作用

时间:2015-01-02 06:46:07

标签: angularjs forms validation

我在这里有一个简单的Angular表单:

<form class="form-horizontal" role="form" name="alertForm">

    <div class="form-group">
        <label for="cautionMultiplier" class="col-sm-2 control-label">Yellow Multiplier</label>
        <div class="col-sm-5">
            <input type="number" min="1" class="form-control" ng-model="alert.cautionMultiplier" id="cautionMultiplier" name="cautionMultiplier" placeholder="Ex. 2" integer required />
            <span>{{alertForm}}</span>
            <span class="alert alert-danger help-block" ng-show="alertForm.cautionMultiplier.$error.number && alertForm.amount.$dirty">
                                    The value is not a valid integer
                                </span>
            <span class="alert alert-danger help-block" ng-show="alertForm.cautionMultiplier.$error.min ">
                                    The value must be at least 1 and an integer
                                </span>
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-2">
            <button type="submit" class="btn btn-primary pull-right" ng-click="addAlert(form)" ng-disabled="alertForm.$invalid">
                Save
            </button>
        </div>
    </div>
</form>

当我在cautionMultiplier框中输入任何非整数(即&#39; a&#39;)时,验证消息The value is not a valid integer不会触发。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

只有在“必需”键出错时才会显示此错误消息:

ng-show="alertForm.cautionMultiplier.$error.required && alertForm.amount.$dirty"

required替换为number

答案 1 :(得分:0)

固定

我的问题是:alertForm.amount.$dirty应该是alertForm.cautionMultiplier.$dirty