Skip to content

Commit

Permalink
Add validation to all input components. close #15
Browse files Browse the repository at this point in the history
  • Loading branch information
heinerwalter committed Sep 18, 2024
1 parent cbad48b commit cea8fdb
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

/.nx

# Compiled output
/dist
/tmp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ <h4>Boolean Input</h4>
<demo-input-demo-wrapper [value]="values.boolean">
<pe-boolean-input label="Boolean input"
infoIconTooltip="Component: <pe-boolean-input>"
helpText="This is an optional help text. You can choose a boolean value here."
[(value)]="values.boolean"
(valueChange)="onInputValueChange('boolean', $event)">
</pe-boolean-input>
Expand All @@ -13,6 +14,7 @@ <h4>Date Input</h4>
<demo-input-demo-wrapper [value]="values.date">
<pe-date-input label="Date input"
infoIconTooltip='Component: <pe-date-input type="date">'
helpText="Help texts work with all kinds of inputs but we will not add them to all inputs on this page."
type="date"
timezone="local"
[(value)]="values.date"
Expand Down Expand Up @@ -337,3 +339,27 @@ <h4>Groups</h4>
</pe-select-input>
</pe-input-group>
</ng-template>

<h4>Validation</h4>
<demo-input-demo-wrapper [value]="values.boolean">
<pe-boolean-input label="Boolean input with validation"
infoIconTooltip='Component: <pe-boolean-input [isValid]="...">'
helpText="All inputs can be displayed as valid (green) or invalid (red) by assigning a boolean value to [isValid]. In this example only the true option is considered as valid."
[required]="true"
[isValid]="values.boolean"
[(value)]="values.boolean"
(valueChange)="onInputValueChange('boolean', $event)">
</pe-boolean-input>
</demo-input-demo-wrapper>

<demo-input-demo-wrapper [value]="values.text">
<pe-text-input label="Text input with validation"
infoIconTooltip='Component: <pe-text-input [isValid]="...">'
[(value)]="values.text"
[required]="true"
[isValid]="!!values.text"
validFeedback="In addition to the validity style of the input element, feedback texts for valid and invalid states can be added like the help text. This is the valid feedback. Clear the input to show the invalid feedback."
invalidFeedback="In addition to the validity style of the input element, feedback texts for valid and invalid states can be added like the help text. This is the invalid feedback. Enter something to show the valid feedback."
(valueChange)="onInputValueChange('text', $event)">
</pe-text-input>
</demo-input-demo-wrapper>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip"
[noFormGroup]="noFormGroup">
<div class="array-form-group-container">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<div class="form-check"
[class.form-switch]="type == 'switch'">
<input class="form-check-input"
[class.is-valid]="isValid == true"
[class.is-invalid]="isValid == false"
type="checkbox"
[attr.role]="type == 'switch' ? 'switch' : 'checkbox'"
[id]="id || label"
Expand All @@ -29,6 +31,10 @@
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText"
[isValid]="isValid"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip"
[noFormGroup]="noFormGroup"
[id]="id"
[name]="name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
[label]="label"
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText">
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</pe-form-group>

Expand All @@ -16,6 +19,7 @@
<pe-boolean-input [noFormGroup]="true"
[id]="id + i"
type="checkbox"
[isValid]="isValid"
[label]="evaluateDisplayPropertyName(item)"
[value]="value?.includes(evaluateValuePropertyName(item))"
(valueChange)="onItemValueChanged(item, $event)">
Expand All @@ -26,6 +30,7 @@
[noFormGroup]="true"
[id]="id + '-select-all'"
type="checkbox"
[isValid]="isValid"
[label]="'Alle auswählen'"
[allowIndeterminate]="true"
[value]="value?.length == 0 ? false : value?.length == dataSource.length ? true : undefined"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
[label]="label"
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText">
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</pe-form-group>

<ng-template #contentTemplate>
<input class="form-control"
[class.is-valid]="isValid == true"
[class.is-invalid]="isValid == false"
[type]="type || 'date'"
[id]="id"
[name]="name || id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
[label]="label"
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText">
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</pe-form-group>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
[label]="label"
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText">
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</pe-form-group>

<ng-template #contentTemplate>
<input class="form-control"
[class.is-valid]="isValid == true"
[class.is-invalid]="isValid == false"
type="file"
[id]="id"
[name]="name || id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
</label>
<div class="form-group-content">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
<div *ngIf="validFeedback"
[class]="'valid-' + (validityFeedbackAsTooltip ? 'tooltip' : 'feedback')">
{{validFeedback}}
</div>
<div *ngIf="invalidFeedback"
[class]="'invalid-' + (validityFeedbackAsTooltip ? 'tooltip' : 'feedback')">
{{invalidFeedback}}
</div>
</div>
<pe-form-text *ngIf="helpText" [id]="for ? for + '-help' : ''">{{helpText}}</pe-form-text>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ export class FormGroupComponent {
*/
@Input() public helpText: string | undefined = undefined;

/**
* If not empty, this text is displayed below the input element (like the `helpText`)
* when the entered value is marked as valid (`isValid == true`).
*/
@Input() public validFeedback: string | undefined = undefined;
/**
* If not empty, this text is displayed below the input element (like the `helpText`)
* when the entered value is marked as invalid (`isValid == false`).
*/
@Input() public invalidFeedback: string | undefined = undefined;
/**
* If false (default), the validity feedback texts (`validFeedback` and `invalidFeedback`)
* are displayed as block element below the content (input element) like the `helpText`.
* If true, the validity feedback texts are displayed as tooltip (not recommended!).
*/
@Input() public validityFeedbackAsTooltip: boolean = false;

/** If true, the input element is not wrapped inside a FormGroup component. */
@Input() public noFormGroup: boolean = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ export class InputBase {
/** If true, an empty value is not valid. */
@Input() public required: boolean = false;

/**
* Apply bootstrap validation style to the input element.
* If `true`, the input element is styled with green border.
* If `false`, the input element is styled with red border.
* If `undefined`, validation is not applied to the input element (default style).
*/
@Input() public isValid: boolean | undefined = undefined;
/**
* Only if a form group component is added (`noFormGroup == false`):
* If not empty, this text is displayed below the input element (like the `helpText`)
* when the entered value is marked as valid (`isValid == true`).
*/
@Input() public validFeedback: string | undefined = undefined;
/**
* Only if a form group component is added (`noFormGroup == false`):
* If not empty, this text is displayed below the input element (like the `helpText`)
* when the entered value is marked as invalid (`isValid == false`).
*/
@Input() public invalidFeedback: string | undefined = undefined;
/**
* Only if a form group component is added (`noFormGroup == false`):
* If false (default), the validity feedback texts (`validFeedback` and `invalidFeedback`)
* are displayed as block element below the content (input element) like the `helpText`.
* If true, the validity feedback texts are displayed as tooltip (not recommended!).
*/
@Input() public validityFeedbackAsTooltip: boolean = false;

/** If true, the input element is not wrapped inside a form group component. */
@Input() public noFormGroup: boolean = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
[label]="label"
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText">
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</pe-form-group>

<ng-template #contentTemplate>
<input class="form-control"
[class.is-valid]="isValid == true"
[class.is-invalid]="isValid == false"
type="number"
[id]="id"
[name]="name || id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
[label]="label"
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText">
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</pe-form-group>

Expand All @@ -18,6 +21,7 @@
[name]="name || id"
[disabled]="disabled"
[readonly]="readonly"
[isValid]="isValid"
[inline]="inline"
[option]="evaluateValuePropertyName(item)"
[(value)]="value"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip"
[noFormGroup]="noFormGroup">
<ngb-rating [id]="id"
[readonly]="readonly"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
[label]="label"
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText">
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</pe-form-group>

<ng-template #contentTemplate>
<select class="form-select"
[class.is-valid]="isValid == true"
[class.is-invalid]="isValid == false"
[class]="selectClass || ''"
[class.readonly]="readonly"
[id]="id"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<div class="form-check"
[class.form-check-inline]="inline">
<input class="form-check-input" type="radio"
[class.is-valid]="isValid == true"
[class.is-invalid]="isValid == false"
[id]="id"
[name]="name || id"
[disabled]="disabled"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
[disabled]="disabled"
[readonly]="readonly"
[required]="required"
[isValid]="isValid"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip"
[noFormGroup]="noFormGroup"
[flexGrow]="flexGrow"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
[label]="label"
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText">
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</pe-form-group>

Expand All @@ -21,7 +24,10 @@
[disabled]="disabled"
[readonly]="readonly"
[required]="required"
[noFormGroup]="noFormGroup"
[isValid]="isValid"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip"
[flexGrow]="flexGrow"

[(value)]="value"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
[label]="label"
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText">
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</pe-form-group>

<ng-template #contentTemplate>
<textarea class="form-control"
[class.is-valid]="isValid == true"
[class.is-invalid]="isValid == false"
[id]="id"
[name]="name || id"
[placeholder]="placeholder || ''"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
[label]="label"
[inlineLabel]="inlineLabel"
[infoIconTooltip]="infoIconTooltip"
[helpText]="helpText">
[helpText]="helpText"
[validFeedback]="validFeedback"
[invalidFeedback]="invalidFeedback"
[validityFeedbackAsTooltip]="validityFeedbackAsTooltip">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</pe-form-group>

<ng-template #contentTemplate>
<input class="form-control"
[class.is-valid]="isValid == true"
[class.is-invalid]="isValid == false"
[type]="type || 'text'"
[id]="id"
[name]="name || id"
Expand Down

0 comments on commit cea8fdb

Please sign in to comment.