Skip to content

Commit

Permalink
Merge pull request #1064 from elwinschmitz/1519-validate-all
Browse files Browse the repository at this point in the history
AB#1519 Fix/refactor issues with validation on all fields + setting answer-data
  • Loading branch information
elwinschmitz authored Aug 4, 2020
2 parents 608e7ca + 1715272 commit 381d41e
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
type="text"
inputmode="numeric"
pattern="[0-9]+"
[name]="name"
[(ngModel)]="ngModel"
[value]="value"
[autocomplete]="autocomplete"
[disabled]="disabled"
[placeholder]="placeholder"
(ionInput)="onInput()"
(ionChange)="onChange()"
ngDefaultControl
required
[required]="required"
></ion-input>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Output,
ViewChild,
} from '@angular/core';
import { NgModel } from '@angular/forms';

@Component({
selector: 'numeric-input',
Expand All @@ -15,9 +16,18 @@ export class NumericInputComponent {
@ViewChild('numericInput')
public numericInput: any;

@Input()
public name: string;

@Input()
public ngModel: NgModel;

@Input()
public value: string;

@Input()
public required: boolean;

@Input()
public autocomplete: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
placeholder="+0 000 000 000"
minlength="8"
maxlength="17"
[name]="name"
[(ngModel)]="ngModel"
[value]="value"
[disabled]="disabled"
(ionChange)="onChange()"
debounce="500"
ngDefaultControl
required
[required]="required"
></ion-input>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Output,
ViewChild,
} from '@angular/core';
import { NgModel } from '@angular/forms';
import { TimeoutError } from 'rxjs';
import { ProgramsServiceApiService } from 'src/app/services/programs-service-api.service';

Expand All @@ -17,12 +18,21 @@ export class PhoneNumberInputComponent {
@ViewChild('telInput')
public telInput: any;

@Input()
public name: string;

@Input()
public ngModel: NgModel;

@Input()
public value: string;

@Input()
public disabled: boolean;

@Input()
public required: boolean;

@Input()
public isValid: boolean;
@Output()
Expand All @@ -40,7 +50,6 @@ export class PhoneNumberInputComponent {
}

public async onChange() {
console.log('onChange: ');
// 'export' the value of the input-ELEMENT to be used as value of this COMPONENT
this.value = this.telInput.value;

Expand Down Expand Up @@ -68,7 +77,7 @@ export class PhoneNumberInputComponent {
isValid = typeof result.result !== 'undefined' ? result.result : false;
},
(error) => {
// If aidworker is offline do not check phonenumber online
// If offline do not check phonenumber online
if (
error.status === 0 ||
error.status === 504 ||
Expand Down
239 changes: 126 additions & 113 deletions interfaces/AW-App/src/app/shared/q-and-a-set/q-and-a-set.component.html
Original file line number Diff line number Diff line change
@@ -1,121 +1,134 @@
<div *ngFor="let question of questions; let i=index; first as isFirst;">
<form #theForm="ngForm">

<dialogue-turn actor="self"
[isConnected]="!isFirst"
[isSpoken]="isFirst || isSubmitted || isEditing"
>
<ion-label>
{{ question.label }}
</ion-label>
<div [ngSwitch]="question.answerType">
<div *ngSwitchCase="answerType.Enum">
<ion-radio-group [name]="question.code"
(ionChange)="onAnswerChange(question.code,$event.target.value)"
debounce="100"
>
<ion-item *ngFor="let option of question.options"
color="light"
lines="full"
<div *ngFor="let question of questions; let i=index; first as isFirst;">
<dialogue-turn
actor="self"
[isConnected]="!isFirst"
[isSpoken]="isFirst || isSubmitted || isEditing"
>
<ion-label>{{ question.label }}</ion-label>
<div [ngSwitch]="question.answerType">
<div *ngSwitchCase="answerType.Enum">
<ion-radio-group
[name]="question.code"
(ionChange)="onAnswerChange(question.code,$event.target.value)"
debounce="100"
>
<ion-label>{{ option.label }}</ion-label>
<ion-radio [value]="option.value"
[checked]="(option.value === answers[question.code]?.value)"
[disabled]="isSubmitted"
></ion-radio>
</ion-item>
</ion-radio-group>
</div>
<div *ngSwitchCase="answerType.Number">
<numeric-input #numInput
ngDefaultControl
[value]="answers[question.code]?.value"
[disabled]="isSubmitted"
(isValidChange)="onChangeWithValidation(question.code, numInput.value, numInput.isValid)"
debounce="300"
></numeric-input>
</div>
<div *ngSwitchCase="answerType.Date">
<ion-input type="date"
placeholder="dd-mm-yyyy"
#dateInput="ngModel"
[(ngModel)]="answerModels[question.code]"
ngDefaultControl
required
[name]="question.code"
[value]="answers[question.code]?.value"
[disabled]="isSubmitted"
(ionChange)="onChangeWithValidation(question.code, dateInput.value, !(dateInput.invalid && (dateInput.dirty || dateInput.touched)))"
debounce="100"
></ion-input>
</div>
<div *ngSwitchCase="answerType.phoneNumber">
<phone-number-input #phoneNumberInput
[value]="answers[question.code]?.value"
ngDefaultControl
[disabled]="isSubmitted"
(isValidChange)="onChangeWithValidation(question.code, phoneNumberInput.value, phoneNumberInput.isValid)"
debounce="500"
></phone-number-input>
</div>
<div *ngSwitchDefault>
<ion-input #defaultInput="ngModel"
[(ngModel)]="answerModels[question.code]"
ngDefaultControl
required
type="text"
pattern=".*\S+.*"
[name]="question.code"
[value]="answers[question.code]?.value"
[disabled]="isSubmitted"
(ionChange)="onChangeWithValidation(question.code, defaultInput.value, !(defaultInput.invalid && (defaultInput.dirty || defaultInput.touched)))"
debounce="300"
></ion-input>
<ion-item
*ngFor="let option of question.options"
color="light"
lines="full"
>
<ion-label>{{ option.label }}</ion-label>
<ion-radio
[value]="option.value"
[checked]="(option.value === answers[question.code]?.value)"
[disabled]="isSubmitted"
></ion-radio>
</ion-item>
</ion-radio-group>
</div>
<div *ngSwitchCase="answerType.Number">
<numeric-input
#numInput
ngDefaultControl
[(ngModel)]="theFormModels[question.code]"
required
[name]="question.code"
[disabled]="isSubmitted"
(isValidChange)="onChangeWithValidation(question.code, numInput.value, numInput.isValid)"
debounce="300"
></numeric-input>
</div>
<div *ngSwitchCase="answerType.Date">
<ion-input
#dateInput="ngModel"
ngDefaultControl
[(ngModel)]="theFormModels[question.code]"
type="date"
placeholder="dd-mm-yyyy"
required
[name]="question.code"
[disabled]="isSubmitted"
(ionChange)="onChangeWithValidation(question.code, dateInput.value, (dateInput.valid && (dateInput.dirty || dateInput.touched)))"
debounce="100"
></ion-input>
</div>
<div *ngSwitchCase="answerType.phoneNumber">
<phone-number-input
#phoneNumberInput
ngDefaultControl
[(ngModel)]="theFormModels[question.code]"
required
[name]="question.code"
[disabled]="isSubmitted"
(isValidChange)="onChangeWithValidation(question.code, phoneNumberInput.value, phoneNumberInput.isValid)"
debounce="500"
></phone-number-input>
</div>
<div *ngSwitchDefault>
<ion-input
#defaultInput="ngModel"
ngDefaultControl
[(ngModel)]="theFormModels[question.code]"
type="text"
pattern=".*\S+.*"
required
[name]="question.code"
[disabled]="isSubmitted"
(ionChange)="onChangeWithValidation(question.code, defaultInput.value, (defaultInput.valid && (defaultInput.dirty || defaultInput.touched)))"
debounce="300"
></ion-input>
</div>
</div>
</div>
</dialogue-turn>

<div *ngIf="checkValidationErrors(question.code)"
class="ion-margin-bottom ion-padding-bottom"
>
<dialogue-turn [isSpoken]="checkValidationErrors(question.code)">
<p *ngIf="(question.answerType === answerType.phoneNumber) && validationErrors.includes('phoneNumber')">
{{ 'q-and-a-set.validation.phone-number'|translate }}
</p>
<p *ngIf="(question.answerType === answerType.Number)">
{{ 'q-and-a-set.validation.number'|translate }}
</p>
<p *ngIf="(question.answerType === answerType.Date)">
{{ 'q-and-a-set.validation.date'|translate }}
</p>
<p *ngIf="(question.answerType === answerType.Text)">
{{ 'q-and-a-set.validation.required'|translate }}
</p>
</dialogue-turn>
<div
*ngIf="checkValidationErrors(question.code)"
class="ion-margin-bottom ion-padding-bottom"
>
<dialogue-turn [isSpoken]="checkValidationErrors(question.code)">
<p *ngIf="(question.answerType === answerType.phoneNumber) && validationErrors.includes('phoneNumber')">
{{ 'q-and-a-set.validation.phone-number'|translate }}
</p>
<p *ngIf="(question.answerType === answerType.Number)">
{{ 'q-and-a-set.validation.number'|translate }}
</p>
<p *ngIf="(question.answerType === answerType.Date)">
{{ 'q-and-a-set.validation.date'|translate }}
</p>
<p *ngIf="(question.answerType === answerType.Text)">
{{ 'q-and-a-set.validation.required'|translate }}
</p>
</dialogue-turn>
</div>
</div>

</div>
<dialogue-turn
*ngIf="((theForm.invalid && (theForm.dirty || theForm.touched)) || checkValidationErrors()) && allQuestionsShown"
[isSpoken]="((theForm.invalid && (theForm.dirty || theForm.touched)) || checkValidationErrors()) && allQuestionsShown"
>
<p>
{{ 'q-and-a-set.validation.generic-error'|translate }}
</p>
</dialogue-turn>

<dialogue-turn *ngIf="checkValidationErrors() && allQuestionsShown"
[isSpoken]="checkValidationErrors() && allQuestionsShown"
>
<p>
{{ 'q-and-a-set.validation.generic-error'|translate }}
</p>
</dialogue-turn>
<dialogue-turn
actor="self"
isConnected="true"
*ngIf="theForm.valid && !checkValidationErrors() && allQuestionsShown"
[isSpoken]="theForm.valid && !checkValidationErrors() && allQuestionsShown"
>
<ion-row class="ion-nowrap ion-align-items-center">
<ion-button
type="submit"
(click)="doSubmit()"
[disabled]="isSubmitted"
expand="block"
style="flex-basis:100%;"
>
{{ submitLabel|translate }}
</ion-button>
</ion-row>
</dialogue-turn>

<dialogue-turn actor="self"
isConnected="true"
*ngIf="!checkValidationErrors() && allQuestionsShown"
[isSpoken]="!checkValidationErrors() && allQuestionsShown"
>
<ion-row class="ion-nowrap ion-align-items-center">
<ion-button type="submit"
(click)="doSubmit()"
[disabled]="isSubmitted"
expand="block"
style="flex-basis:100%;"
>
{{ submitLabel|translate }}
</ion-button>
</ion-row>
</dialogue-turn>
</form>
Loading

0 comments on commit 381d41e

Please sign in to comment.