我有Custom input component with validation ngMessages,FormController和ng-required:
<div class="col-sm-9 col-xs-12">
<input
id="{{$ctrl.fieldName}}"
name="{{$ctrl.fieldName}}"
class="form-control"
type="text"
minlength="{{$ctrl.minLength}}"
maxlength="{{$ctrl.maxLength}}"
ng-required="{{$ctrl.isRequired === 'true'}}"
ng-model="$ctrl.value"
ng-change="$ctrl.form.$submitted = false;"
>
<div
ng-messages="$ctrl.form[$ctrl.fieldName].$error"
ng-if="$ctrl.form.$submitted"
>
<span class="help-block" ng-message="required">
Field is required
</span>
<span class="help-block" ng-message="minlength">
Minimum length of field: {{$ctrl.minLength}}
</span>
<span class="help-block" ng-message="maxlength">
Maximum length of field: {{$ctrl.maxLength}}
</span>
</div>
</div>
以这种方式使用:
<act-text-field
form="heroAddForm"
field-name="name"
min-length="3"
max-length="15"
is-required="true"
errors="$ctrl.errors.name"
ng-model="$ctrl.hero.name">
</act-text-field>
当用户点击submit
按钮时,我想要实现的是验证。并且它可以运行,验证也会针对必填字段name
,但也针对字段description
,这不是必需的:
<act-text-field
form="heroAddForm"
field-name="description"
max-length="50"
is-required="false"
errors="$ctrl.errors.description"
ng-model="$ctrl.hero.description"
></act-text-field>
此字段验证消息也可见,但字段description
有效,因为我将类has-error
添加到无效字段:
<div class="form-group"
ng-class="{'has-error': $ctrl.form.$submitted && (!$ctrl.form[$ctrl.fieldName].$valid)}"
>
<!-- rest of code -->
您可以轻松地在我的Plunker: Custom input demo app with validation states中重现这种错误的行为(我知道它有其他错误)。我认为ng-message="required"
不应该是可见的,因为不需要字段description
。我知道我可以在代码中添加一些ng-if
来绕过它,但我认为我在某个地方犯了一个错误,我无法看到。你看到我弄错了吗?提前感谢您的帮助。