Browse Source

Lwm2m: front add validator json for security config value

pull/4133/head
nickAS21 6 years ago
parent
commit
2452aefff9
  1. 3
      ui-ngx/src/app/modules/home/components/device/device-credentials.component.html
  2. 28
      ui-ngx/src/app/modules/home/components/device/device-credentials.component.ts
  3. 1
      ui-ngx/src/assets/locale/locale.constant-en_US.json

3
ui-ngx/src/app/modules/home/components/device/device-credentials.component.html

@ -88,6 +88,9 @@
<mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('required')">
{{ 'device.lwm2m-value-required' | translate }}
</mat-error>
<mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('jsonError')">
{{ 'device.lwm2m-value-json-error' | translate }}
</mat-error>
<div mat-dialog-actions fxLayoutAlign="center center">
<button mat-raised-button color="primary"
matTooltip="{{'device.lwm2m-value-edit-tip' | translate }}"

28
ui-ngx/src/app/modules/home/components/device/device-credentials.component.ts

@ -196,13 +196,19 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false});
break;
case DeviceCredentialsType.X509_CERTIFICATE:
case DeviceCredentialsType.LWM2M_CREDENTIALS:
this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required]);
this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false});
this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]);
this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false});
this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false});
break;
case DeviceCredentialsType.LWM2M_CREDENTIALS:
this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required, this.jsonValidator]);
this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false});
this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]);
this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false});
this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false});
break;
case DeviceCredentialsType.MQTT_BASIC:
this.deviceCredentialsFormGroup.get('credentialsBasic').enable({emitEvent: false});
this.deviceCredentialsFormGroup.get('credentialsBasic').updateValueAndValidity({emitEvent: false});
@ -247,7 +253,11 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
if (credentialsValue === null || credentialsValue.length === 0) {
credentialsValue = getDefaultSecurityConfig();
} else {
credentialsValue = JSON.parse(credentialsValue);
try {
credentialsValue = JSON.parse(credentialsValue);
} catch (e) {
credentialsValue = getDefaultSecurityConfig();
}
}
const credentialsId = this.deviceCredentialsFormGroup.get('credentialsId').value || DEFAULT_END_POINT;
this.dialog.open<SecurityConfigComponent, DeviceCredentialsDialogLwm2mData, object>(SecurityConfigComponent, {
@ -273,4 +283,18 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
private isDefautLw2mResponse(response: object): boolean {
return Object.keys(response).length === 0 || JSON.stringify(response) === '[{}]';
}
private jsonValidator(control: FormControl) {
try {
debugger;
JSON.parse(control.value);
return null
} catch (e) {
return {
jsonError: {
parsedJson: "error"
}
}
}
}
}

1
ui-ngx/src/assets/locale/locale.constant-en_US.json

@ -881,6 +881,7 @@
"lwm2m-key-required": "LwM2M Security config key is required.",
"lwm2m-value": "LwM2M Security config",
"lwm2m-value-required": "LwM2M Security config value is required.",
"lwm2m-value-json-error": "LwM2M Security config value is not json format.",
"lwm2m-endpoint": "Client endpoint/identity",
"lwm2m-security-info": "Security Config Info",
"lwm2m-value-edit": "Edit Security config",

Loading…
Cancel
Save