gpt4 book ai didi

angular - 无法使用 Angular 为字段设置动态验证

转载 作者:太空狗 更新时间:2023-10-29 17:14:42 26 4
gpt4 key购买 nike

我遇到了现有应用程序的问题,以下是我的情况我有以下 JSON 格式

.html代码

<div class="panel-group" id="accordion">
<div *ngFor="let property of Tree.properties">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="link" data-toggle="collapse" data-parent="#accordion" href="#dataCatg-{{property.name}}">
<div *ngIf="property.required">
<span class="glyphicon glyphicon-chevron-right"></span>{{property.name}}
</div>
<div *ngIf="!property.required">
<span class="glyphicon glyphicon-chevron-right"></span>{{property.name}}
</div>
</a>
</h4>
</div>
<div id="dataCatg-{{property.name}}" class="panel-collapse collapse">
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item" *ngFor="let prop of property.details">
<div *ngIf="prop.details.visible">
<div class="row">
<div class="col-md-4">
<div *ngIf="data.includes(prop.name)">
<label class="inline-label" for="{{prop.name}}">{{prop.name}}</label>
</div>
<div *ngIf="!data.includes(prop.name) ">
<label class="inline-label " for="{{prop.name}} ">{{prop.name}}</label>
</div>
</div>
<div class="col-md-8 ">
<div *ngIf="!Edit">
<span *ngIf="formVisible && metaDataTemplateMap[selectedFile]!==undefined ">
<input id="{{prop.name}}" type="{{prop.details.type}} " [(ngModel)]="Data[prop.name]" class="form-control ">
</span>
</div>
<div *ngIf="Edit">
<div *ngIf="prop.details.group ">
<span *ngIf="formView">
<!--need-->
<input id="{{prop.name}}" type="{{prop.details.type}}" [(ngModel)]="Edit[prop.name]" (ngModelChange)="Edit($event)" style=" border-radius:0;"
class="form-control">
</span>
</div>
<div *ngIf="!prop.details.group ">
<input id="{{prop.name}}" type="text " style=" border-radius:0" class="form-control " readonly>
</div>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>

.ts代码

Data(res) {


this.Tree['Properties'] = [];

for (let property in res.properties) {

var prop = res.properties[property];
if (prop['properties'] !== undefined) {
let temp= {};
if (res['required'].indexOf(property) !== -1) {
temp['required'] = true;
}
else {
temp['required'] = false;
}
temp['name'] = property;
let template = {};
temp['details'] = [];
for (let nestedProps in prop.properties) {
let nestedProp = {};
nestedProp['name'] = nestedProps;

if (prop.properties[nestedProps]['type'] == 'string' || prop.properties[nestedProps]['type'] == 'date-time') {
prop.properties[nestedProps]['type'] = 'text';
template[nestedProps] = '';
}

if (prop.properties[nestedProps]['type'] == 'integer') {
prop.properties[nestedProps]['type'] = 'number';
template[nestedProps] = 0;
}

if (prop.properties[nestedProps]['type'] == 'array') {
prop.properties[nestedProps]['type'] = 'array';
template[nestedProps] = '';
}
if (prop.properties[nestedProps]['group'] == true) {
if (this.Edit[property] == undefined)
this.Edit[property] = {};
this.Edit[property][nestedProps] = '';
}

nestedProp['details'] = prop.properties[nestedProps];

temp['details'].push(nestedProp);
}
this.Data[property] = template;
this.Tree['Properties'].push(temp);
}
if (prop['properties'] == undefined) {
let temp = {};
if (res['required'].indexOf(property) !== -1) {
temp['required'] = true;
}
else {
temp['required'] = false;
}
temp['name'] = property;
if (prop['type'] == 'string' || prop['type'] == 'date-time') {
prop['type'] = 'text';
this.Data[property] = '';
}

if (prop['type'] == 'number') {
prop['type'] = 'integer';
this.Data[property] = 0;
}

if (prop['group'] == true) {
this.Edit[property] = '';
}
temp['details'] = prop;
this.Tree['Others'].push(temp);
}
}


}

这里我想要的是 1. 如果你在 JSON 中看到我有

"required": [
"host",
"quantity",
"id"
],

在生成字段时,它必须检查上面提到的字段是否为空,如果没有模板或 react 形式方法,如果字段为空,那么我们怎么可能让用户知道字段为空,我该如何完成这?

最佳答案

Aim

在没有ReactiveTemplate 方法的情况下验证动态生成的表单输入。

Solution

Directive 最适合此类需求。 指令有助于将复杂的工作分解成小的独立任务。让我们看看它是如何实现的。

下面提供的实现不需要对现有代码进行任何更改。

1。验证服务

import { Injectable } from '@angular/core';

@Injectable()
export class ValidateService {

errors = {};

validate(key: string, value: object) {
this.errors[key] = value;
}

getErrors() {
let errorList = [];
Object.keys(this.errors).forEach(key => {
let value = this.errors[key];
if ((value == undefined || value == '') && this.required.find(item=>item == key)) {
errorList.push({ name: key, error: key + " Field is required" })
}
});
return errorList;
}

//All required fields can be maintained here
required = [
"host",
"quantity",
"id"
]

}

2。验证指令

ValidateDirective 负责收集 input 控件的当前值,如果发生任何变化。此信息将传递给服务类 ValidationService

import { Directive, Host, Input, OnChanges, SimpleChanges, ViewContainerRef, AfterViewInit } from '@angular/core';
import { ValidateService } from './validate.service';

@Directive({
selector: '[validate]'
})
export class ValidateDirective implements OnChanges {

constructor(private service: ValidateService, private containerRef: ViewContainerRef) {

}

@Input("ngModel") model;

@Input("validate") element;

ngOnChanges(changes: SimpleChanges) {

setTimeout(() => {
this.service.validate(this.containerRef.element.nativeElement.id, changes.model.currentValue);
})
}

}

3。指令用法

ValidateDirective 可以与任何具有 idngModel 的输入控件一起使用。

例如:

<input [validate] id="{{prop.name}}" type="{{prop.details.type}}" [(ngModel)]="Edit[prop.name]" (ngModelChange)="Edit($event)" style=" border-radius:0;" class="form-control">

4。组件 ts

ValidateService 将被注入(inject)到组件中以获取错误列表。

constructor(private service:ValidateService) {}

public get errors(){
return this.service.getErrors();
}

5。显示错误

由于所有错误都在Component中,所以可以在html中显示。

例如:

 <li *ngFor="let error of errors">
{{error.error}}
</li>

Note - There are many thing which can be enhanced further like

  1. Passing custom message to Directive.
  2. Required field list can be passed to Directive as @Input

工作示例演示在这里 - https://stackblitz.com/edit/angular-xnbzqd

关于angular - 无法使用 Angular 为字段设置动态验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52922079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com