-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in feature/SHIBUI-907 (pull request #277)
SHIBUI-907 - Fixes issues with required property validation messages Approved-by: Jonathan Johnson <jj@scaldingspoon.com>
- Loading branch information
Showing
18 changed files
with
176 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const HARD_CODED_REQUIRED_MSG = RegExp('Missing required property'); | ||
|
||
export const REQUIRED_MSG_OVERRIDE = 'message.required'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as ZSchema from 'z-schema'; | ||
import { ZSchemaValidatorFactory } from 'ngx-schema-form'; | ||
|
||
export class CustomSchemaValidatorFactory extends ZSchemaValidatorFactory { | ||
|
||
protected zschema; | ||
|
||
constructor() { | ||
super(); | ||
this.createSchemaValidator(); | ||
} | ||
|
||
private createSchemaValidator() { | ||
this.zschema = new ZSchema({ | ||
breakOnFirstError: false | ||
}); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,54 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, AfterViewInit, OnDestroy } from '@angular/core'; | ||
import { StringWidget } from 'ngx-schema-form'; | ||
import { Validators } from '@angular/forms'; | ||
import { SchemaService } from '../../service/schema.service'; | ||
import { startWith } from 'rxjs/operators'; | ||
import { Subscription } from 'rxjs'; | ||
|
||
import { HARD_CODED_REQUIRED_MSG, REQUIRED_MSG_OVERRIDE } from '../../model/messages'; | ||
|
||
@Component({ | ||
selector: 'custom-string', | ||
templateUrl: `./string.component.html`, | ||
styleUrls: ['../widget.component.scss'] | ||
}) | ||
export class CustomStringComponent extends StringWidget { | ||
export class CustomStringComponent extends StringWidget implements AfterViewInit, OnDestroy { | ||
|
||
errorSub: Subscription; | ||
|
||
constructor( | ||
private widgetService: SchemaService | ||
) { | ||
super(); | ||
} | ||
|
||
ngAfterViewInit(): void { | ||
super.ngAfterViewInit(); | ||
let listener = this.formProperty.parent ? this.formProperty.parent : this.control; | ||
this.errorSub = listener.valueChanges.pipe(startWith(listener.value)).subscribe(v => { | ||
if (!this.control.value | ||
&& this.required | ||
&& !this.errorMessages.some(msg => HARD_CODED_REQUIRED_MSG.test(msg)) | ||
&& this.errorMessages.indexOf(REQUIRED_MSG_OVERRIDE) < 0) { | ||
this.errorMessages.push(REQUIRED_MSG_OVERRIDE); | ||
} | ||
if (!this.required) { | ||
this.errorMessages = this.errorMessages.filter(e => e !== REQUIRED_MSG_OVERRIDE); | ||
} | ||
}); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.errorSub.unsubscribe(); | ||
} | ||
|
||
get required(): boolean { | ||
return this.widgetService.isRequired(this.formProperty); | ||
const req = this.widgetService.isRequired(this.formProperty); | ||
|
||
return req; | ||
} | ||
|
||
getError(error: string): string { | ||
return HARD_CODED_REQUIRED_MSG.test(error) ? REQUIRED_MSG_OVERRIDE : error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 25 additions & 2 deletions
27
ui/src/app/schema-form/widget/textarea/textarea.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,43 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, AfterViewInit, OnDestroy } from '@angular/core'; | ||
|
||
import { TextAreaWidget } from 'ngx-schema-form'; | ||
import { SchemaService } from '../../service/schema.service'; | ||
import { Subscription } from 'rxjs'; | ||
import { startWith } from 'rxjs/operators'; | ||
import { HARD_CODED_REQUIRED_MSG } from '../../model/messages'; | ||
|
||
@Component({ | ||
selector: 'textarea-component', | ||
templateUrl: `./textarea.component.html` | ||
}) | ||
export class CustomTextAreaComponent extends TextAreaWidget { | ||
export class CustomTextAreaComponent extends TextAreaWidget implements AfterViewInit, OnDestroy { | ||
|
||
errorSub: Subscription; | ||
|
||
constructor( | ||
private widgetService: SchemaService | ||
) { | ||
super(); | ||
} | ||
|
||
ngAfterViewInit(): void { | ||
super.ngAfterViewInit(); | ||
this.errorSub = this.control.valueChanges.pipe(startWith(this.control.value)).subscribe(v => { | ||
if (!v && this.required && !this.errorMessages.some(msg => HARD_CODED_REQUIRED_MSG.test(msg))) { | ||
this.errorMessages.push('message.required'); | ||
} | ||
}); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.errorSub.unsubscribe(); | ||
} | ||
|
||
get required(): boolean { | ||
return this.widgetService.isRequired(this.formProperty); | ||
} | ||
|
||
getError(error: string): string { | ||
return error.match('required').length ? 'message.required' : error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters