[英]Required field validation for kendo-dropdownlist in angular 7 application
我正在尝试在我的模板驱动表单中以angular 7形式对kendo-dropdownlist进行必需的字段验证。如果您看到我正在循环并生成一个动态表,该表在每行中都有kendo dropdownlist。 如果未选中下拉菜单,则需要突出显示。 我试图用div标签将div括起来,以为当用户按下Submit时可以处理它,但是我认为这更多是在kendo中进行设置。 有人可以告诉我这样做的方法。 到目前为止,无论我看到的任何示例都是基于jquery的。
这是stackblitz https://stackblitz.com/edit/angular-4v2k8f
HTML
form name="form" (ngSubmit)="f.form.valid && createDocument()" #f="ngForm" novalidate>
<div class="center" class="file-upload-wrapper">
<ngx-file-drop dropZoneLabel="Drop files here" dropZoneClassName="file-drop" multiple="true"
(onFileDrop)="dropped($event)" (onFileOver)="fileOver($event)" (onFileLeave)="fileLeave($event)">
<ng-template ngx-file-drop-content-tmp let-openFileSelector="openFileSelector">
<button type="button" (click)="openFileSelector()">Drop Files to Upload</button>
</ng-template>
</ngx-file-drop>
<div class="upload-table">
<table id="table1" class="center">
<tbody class="upload-name-style">
<tr *ngFor="let item of files; let i=index">
<td> <input kendoTextBox [(ngModel)]="item.relativePath" style="width: 350px" /></td>
<td>
<kendo-dropdownlist style="width:350px" [(ngModel)]="item.selectedDocumentItem"
[data]="DocumentTypes" [defaultItem]="defaultItem" [filterable]="false" textField="Name"
valueField="Id">
</kendo-dropdownlist>
</td>
<td>
<kendo-datepicker style="width: 200px" [format]="'dd MMM, yyyy'"
[(ngModel)]="item.selectedDate"></kendo-datepicker>
</td>
<td> <button class="btn btn-default" (click)="deleteRow(i)"><i class="fa fa-trash"></i>Delete
</button></td>
</tr>
</tbody>
</table>
</div>
<div class="wrapper">
<button *ngIf="files.length > 0" type="submit" class="btn btn-primary btn-upload">upload</button>
</div>
</div>
</form>
零件
public createDocument() {
this.files.forEach(element => {
this.uploadDocument = <IDocument>{
id: 5508,
documentTypeId: element.selectedDocumentItem.Id ,
name: element.relativePath,
documentDate: element.selectedDate
};
});
}
您唯一需要的是使用引用变量,查看其是否有效。 我在stackblitz中举了一个简单的例子。 因为您只想知道是否具有有效,所以可以使用简单必需的。 我的堆积如山
更新 :以表格形式,禁用按钮提交
@Component({
selector: 'my-app',
template: `
<div class="example-wrapper">
<form #myForm="ngForm">
<div *ngFor="let item of files; let i=index">
<p>T-shirt size:</p>
<kendo-dropdownlist name="select{{i}}" #name="ngModel" [(ngModel)]="value[i]" [data]="listItems" required>
</kendo-dropdownlist>
<span *ngIf="name.invalid">*</span>
</div>
<button [disabled]="myForm.invalid">submit</button>
</form>
</div>
`
})
export class AppComponent {
public listItems: Array<string> = ["X-Small", "Small", "Medium", "Large", "X-Large", "2X-Large"];
value=[]
files=[{value:''},{value:''},{value:''}]
}
简要说明(但您有docs )当我们有[[ngModel)]时,我们可以使用模板引用来引用输入。 如果我们将模板引用与ngModel #name="ngModel"
,则可以在.html中使用template变量和ngModel的所有属性(invalid,touched ...),方法为name.invalid
, name.touched
...
啊!,不用担心您会放置“相同”参考变量,如果模型的变量不相等,Angular就会知道它们是不同的变量。
注意:就个人而言,我建议您使用ReactiveForms和FormArray,但这只是一个意见。
确实是Update 2问题是您无法循环访问要修改的同一列表。 您有*ngFor="let item of files"
并且您正在更改files
。 诀窍是遍历' '.repeat(files.length).split('')
-或在代码中创建一个数组this.iterator=new Array(this.files.length)
,然后可以执行<tr *ngFor="let t of iterator;let i=index">
[[ngModel)]在文件[i] .selectedDocumentItem.Id上
参见stackblitz和代码
<div class="example-wrapper">
<form #myForm="ngForm">
<!-- other way is <tr *ngFor="let t of iterator;let i=index"> -->
<tr *ngFor="let t of ' '.repeat(files.length).split(''); let i=index">
<td>
<kendo-dropdownlist name="select{{i}}" #name="ngModel" [(ngModel)]="files[i].selectedDocumentItem.Id" [defaultItem]="files[i].selectedDocumentItem.id"
[data]="DocumentTypes" [valuePrimitive]="true" textField="Name" valueField="Id" required>
</kendo-dropdownlist>
<span *ngIf="name.invalid">*</span>
</td>
</tr>
<button [disabled]="myForm.invalid">submit</button>
</form>
</div>
如果要用作[ngModel] = files [i] .selectedDocumentItem(一个对象),则需要创建一个customError指令。 看起来像
@Directive({
selector: '[requiredId]',
providers: [{provide: NG_VALIDATORS, useExisting: RequiredIdDirective, multi: true}]
})
export class RequiredIdDirective implements Validator {
validate(control: AbstractControl): {[key: string]: any} | null {
return control.value.Id?null:{required:true}
}
}
现在我们的下拉列表就像
<kendo-dropdownlist name="select{{i}}" #name="ngModel"
[(ngModel)]="files[i].selectedDocumentItem"
[defaultItem]="files[i].selectedDocumentItem"
[data]="DocumentTypes"
textField="Name" valueField="Id" requiredId>
</kendo-dropdownlist>
看到新的堆叠闪电战
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.