当我们将鼠标悬停在表格的第一列上时,会出现一个工具提示,然后点击按钮,打开工具提示垫对话框中的presnt。
该对话框包含左两部分和Edit json和。在左边,选择了哪一行,右边显示了json形式的相应数据。
1( 基本的表单验证是有效的,但我需要显示一些消息(如果表单无效(,以防用户编辑json并尝试单击按钮。(尝试在Update属性上使用blur[ngModelOption][ngFormOption],但无法实现(
对于json部分,基本验证也有效,但对于以下几点,如何提供验证:
1( 如果我把一个键设为空字符串,它不会验证。
2( 如何强制使用特定密钥。
Stackblitz链接https://stackblitz.com/edit/angular-mat-tooltip-qxxgcp?file=app%2Falert-对话框%2Alert-dialog.component.html
摘录自alert-dialog.component.ts
<form #jsonform="ngForm" (ngSubmit)="onAddNewAlert()">
<json-input [(ngModel)]="data.data[incomingSelectedAlert].conditionals" name="result"></json-input>
<button type="submit" class="btn btn-success alertButtonSubmit" [disabled]="jsonform.invalid">Add As New Alert</button>
</form>
json-input.component(我有NG_VALIDATORS和valdidate函数来执行基本的json验证(
@Component({
selector: 'json-input',
template:
`
<style>
textarea {
height: 421px;
width: 480px;
resize: none;
}
</style>
<textarea
[value]="jsonString"
(change)="onChange($event)"
(keyup)="onChange($event)">
</textarea>
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => JsonInputComponent),
multi: true,
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => JsonInputComponent),
multi: true,
}]
})
export class JsonInputComponent implements ControlValueAccessor, Validator {
private jsonString: string;
private parseError: boolean;
private data: any;
// this is the initial value set to the component
public writeValue(obj: any) {
if (obj) {
this.data = obj;
// this will format it with 4 character spacing
this.jsonString = JSON.stringify(this.data, undefined, 4);
}
}
// registers 'fn' that will be fired wheb changes are made
// this is how we emit the changes back to the form
public registerOnChange(fn: any) {
this.propagateChange = fn;
}
// validates the form, returns null when valid else the validation object
// in this case we're checking if the json parsing has passed or failed from the onChange method
public validate(c: FormControl) {
return (!this.parseError) ? null : {
jsonParseError: {
valid: false,
},
};
}
// not used, used for touch input
public registerOnTouched() { }
// change events from the textarea
private onChange(event) {
// get value from text area
let newValue = event.target.value;
try {
// parse it to json
this.data = JSON.parse(newValue);
this.parseError = false;
} catch (ex) {
// set parse error if it fails
this.parseError = true;
}
// update the form
this.propagateChange(this.data);
}
// the method set in registerOnChange to emit changes back to the form
private propagateChange = (_: any) => { };
}
我已经完成了您需要的以下步骤:
- 将json表单转换为反应表单,而不是模板表单(使用模板表单并在TypeScript代码中检索
NgForm
指令不是一个好的做法( -
将
forbiddenFields
(包含空字符串,因为它是JS中的实际有效密钥(和mandatoryFields
添加到JsonInputComponent
mandatoryFields: string[] = ["conditionals", "offset", "alert"]; forbiddenFields: string[] = [""];
-
在
onChange
功能中执行字段检查:private onChange(event) { // get value from text area let newValue = event.target.value; try { // parse it to json this.data = JSON.parse(newValue); const datakeys = Object.keys(this.data); const containsForbiddenFields = datakeys.some(x => this.forbiddenFields.indexOf(x) >= 0); const containsMandatoryFields = this.mandatoryFields.every(x => datakeys.indexOf(x) >= 0); this.parseError = containsForbiddenFields || !containsMandatoryFields; } catch (ex) { // set parse error if it fails this.parseError = true; } // update the form this.propagateChange(this.data); }
对于完整的工作解决方案,这里是分叉的Stacklitz
我遵循了以下步骤,并根据要求更新了您的代码,它似乎运行良好:
用于添加验证以检查中的空字符串和强制键嵌套对象,当值为在控件值的帮助下AlertDialogComponent中的表单更改访问者。
添加了这些变量来验证表单并获取JSON对象、的密钥
isFormValid = true;
mandatoryField = ['conditionals', 'offset', 'alert', 'mTitle'];
allMandatoryFieldsExists: boolean;
在这里附上我的解决方案和主要代码,Stacklitz
this.jsonform.statusChanges.subscribe(() => {
let data = this.jsonform.value.result;
let keys = [];
if(data) {
for(var i in data) {
keys.push(i);
if(i === 'offset' && data[i] === '') {
this.isFormValid = false;
return;
} else {
this.isFormValid = true;
}
if(i === 'conditionals') {
for(var j in data[i][0]) {
keys.push(j);
if(data[i][0][j] === '') {
this.isFormValid = false;
return;
} else {
this.isFormValid = true;
}
}
}
if(i === 'alert') {
for(var j in data[i]) {
keys.push(j);
if(data[i][j] === '') {
this.isFormValid = false;
return;
} else {
this.isFormValid = true;
}
}
}
}
this.allMandatoryFieldsExists = this.mandatoryField.every(x => keys.indexOf(x) >= 0);
}
if(this.jsonform.dirty && (!this.isFormValid || !this.allMandatoryFieldsExists)){
console.log("form is dirty and not valid")
// this.alertService.error("Json not correct. Please fix!");
}else{
console.log("form is dirty but valid")
}
});