Skip to content

Commit

Permalink
Merge pull request #176 from gctools-outilsgc/editor-update
Browse files Browse the repository at this point in the history
Editor validation
  • Loading branch information
doug0102 authored Nov 7, 2023
2 parents d5bf990 + d86a27f commit 94b8d90
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ <h1 *ngIf="!loading">
class="registerBtn"
[disabled]="loading || isPast()"
[ngClass]="loading || isPast() ? 'disabled' : 'enabled'"
theme="secondary-2"
(click)="scrollToRegister()">
{{ translations.event.form.register | translate }}
</app-button>
Expand Down
6 changes: 5 additions & 1 deletion src/app/shared/components/blog-form/blog-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FormControl, FormGroup, FormGroupDirective, NgForm, Validators } from '
import { ErrorStateMatcher } from '@angular/material/core';
import { TranslateService } from '@ngx-translate/core';
import { Translations } from 'src/app/core/services/translations.service';
import { Validators as EditorValidators } from 'ngx-editor';

@Component({
selector: 'app-blog-form',
Expand Down Expand Up @@ -31,7 +32,10 @@ export class BlogFormComponent implements OnInit, OnDestroy {
ngOnInit(): void {
for (const [key, value] of Object.entries(this.model)) {
if (!this.form.controls[key]) {
this.form.addControl(key, new FormControl(value, [Validators.required]));
if (key == 'description')
this.form.addControl(key, new FormControl(value, [EditorValidators.required(), EditorValidators.maxLength(this.maxBlogLength)]));
else
this.form.addControl(key, new FormControl(value, [Validators.required]));
} else {
this.form.controls[key].setValue(value);
console.warn('Duplicate FormControl detected.');
Expand Down
6 changes: 3 additions & 3 deletions src/app/shared/components/editor/editor.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</mat-label>

<div #gccEditor class="NgxEditor__Wrapper"
[ngClass]="{focus: hasFocus, error: control && control.hasError('required') || (characterCount > maxCharacters) || (characterCount < minCharacters)}">
[ngClass]="{focus: hasFocus, error: control && control.touched && !control.valid}">
<ngx-editor-menu
[editor]="editor"
[toolbar]="toolbar"
Expand All @@ -29,7 +29,7 @@
[required]="required">
</ngx-editor>
<div *ngIf="minCharacters > 0 || maxCharacters != Number.MAX_VALUE"
[ngClass]="(characterCount > maxCharacters) || (characterCount < minCharacters) ? 'error' : ''"
[ngClass]="(control && control.touched) && ((characterCount > maxCharacters) || (characterCount < minCharacters)) ? 'error' : ''"
class="character-count">
{{ characterCount }} / {{ maxCharacters }}
</div>
Expand All @@ -47,7 +47,7 @@
{{ translations.input.error.maxLength | translate }} {{ control.errors!['maxlength'].requiredLength }}.
</mat-error>

<mat-error *ngIf="control && control.hasError('required')">
<mat-error *ngIf="control && control.touched && control.hasError('required')">
{{ label }} {{ translations.input.error.required | translate }}
</mat-error>

Expand Down
39 changes: 8 additions & 31 deletions src/app/shared/components/editor/editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Editor, NgxEditorService, Toolbar } from 'ngx-editor';
import { ngxEditorLocals } from '../../factories/editor-config.factory';
import { Subscription } from 'rxjs/internal/Subscription';
import { Translations } from 'src/app/core/services/translations.service';
import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR } from '@angular/forms';
import { TooltipDirection } from '../../models/tooltip-direction';

// https://sibiraj-s.github.io/ngx-editor/en/introduction/
Expand All @@ -27,10 +27,10 @@ export class EditorComponent implements OnInit, OnDestroy, AfterViewInit, AfterC
@Input() label!: string;
@Input() hint!: string;
@Input() autofocus: boolean = false;
@Input() minCharacters: number = 0;
@Input() maxCharacters: number = Number.MAX_VALUE;
@Input() control!: FormControl;
@Input() controlName!: string;
@Input() minCharacters: number = 0;
@Input() maxCharacters: number = Number.MAX_VALUE;

@Output() htmlChange = new EventEmitter<string>();

Expand Down Expand Up @@ -92,14 +92,9 @@ export class EditorComponent implements OnInit, OnDestroy, AfterViewInit, AfterC
}
}

// TODO: Handle min/max
ngAfterContentInit(): void {
if (this.control && this.control.hasValidator(Validators.required)) {
this.control.removeValidators([Validators.required]);
this.control.clearValidators();
this.control.updateValueAndValidity();
this.hasRequiredValidator = true;
}
if (this.control && this.html != '')
this.updateCharacterCount(this.html);
}

ngAfterViewInit() {
Expand All @@ -111,12 +106,8 @@ export class EditorComponent implements OnInit, OnDestroy, AfterViewInit, AfterC
if (classList) {
this.hasFocus = Array.from(classList).includes('ProseMirror-focused') ? true : false;

// TODO
if (!this.hasFocus && this.control && !this.control.hasValidator(Validators.required) && this.hasRequiredValidator) {
this.control.addValidators([Validators.required]);
this.control.updateValueAndValidity();
this.updateCharacterCount(this.html);
}
if (this.control && !this.hasFocus)
this.control.markAsTouched();
}
});
});
Expand Down Expand Up @@ -151,22 +142,8 @@ export class EditorComponent implements OnInit, OnDestroy, AfterViewInit, AfterC
this.onInputChange(this.html);
}

if (this.control) {
if (this.characterCount > this.maxCharacters){
this.control.setErrors({
maxlength: {
requiredLength: this.maxCharacters
}
});
} else if (this.characterCount < this.minCharacters) {
this.control.setErrors({
minlength: {
requiredLength: this.minCharacters
}
});
}
if (this.control)
this.control.markAsTouched();
}
}

onLangChange(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
[hint]="translations.forms.event.body_hint | translate"
[(html)]="model.eventDescription"
[required]="true"
[maxCharacters]="240"
[maxCharacters]="maxCharacters"
formControlName="eventDescription"
[control]="$any(form).controls['eventDescription']"
[disabled]="disabled">
Expand Down
7 changes: 6 additions & 1 deletion src/app/shared/components/event-form/event-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FormControl, FormGroup, FormGroupDirective, NgForm, Validators } from '
import { ErrorStateMatcher } from '@angular/material/core';
import { TranslateService } from '@ngx-translate/core';
import { Translations } from 'src/app/core/services/translations.service';
import { Validators as EditorValidators } from 'ngx-editor';

@Component({
selector: 'app-event-form',
Expand Down Expand Up @@ -31,6 +32,7 @@ export class EventFormComponent implements OnInit, OnDestroy {
eventLanguage = EventLanguage;
eventDuration = EventDuration;
errorStateMatcher = new MyErrorStateMatcher();
maxCharacters = 240;

constructor(public translations: Translations) {

Expand All @@ -39,7 +41,10 @@ export class EventFormComponent implements OnInit, OnDestroy {
ngOnInit(): void {
for (const [key, value] of Object.entries(this.model)) {
if (!this.form.controls[key]) {
this.form.addControl(key, new FormControl(value, [Validators.required]));
if (key == 'eventDescription')
this.form.addControl(key, new FormControl(value, [EditorValidators.required(), EditorValidators.maxLength(this.maxCharacters)]));
else
this.form.addControl(key, new FormControl(value, [Validators.required]));
} else {
this.form.controls[key].setValue(value);
console.warn('Duplicate FormControl detected.');
Expand Down
6 changes: 3 additions & 3 deletions src/app/shared/components/poll-form/poll-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FormControl, FormGroup, FormGroupDirective, NgForm, Validators } from '
import { ErrorStateMatcher } from '@angular/material/core';
import { TranslateService } from '@ngx-translate/core';
import { Translations } from 'src/app/core/services/translations.service';
import { Validators as EditorValidators } from 'ngx-editor';

@Component({
selector: 'app-poll-form',
Expand Down Expand Up @@ -38,9 +39,8 @@ export class PollFormComponent implements OnInit, OnDestroy {
new FormControl(
this.model.description,
[
Validators.required,
//Validators.minLength(this.minLength),
Validators.maxLength(this.maxLength),
EditorValidators.required(),
EditorValidators.maxLength(this.maxLength),
]
)
);
Expand Down
8 changes: 4 additions & 4 deletions src/app/shared/components/post-form/post-form.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<app-editor [label]="translations.forms.post.post | translate"
[hint]="translations.forms.placeholder | translate"
[(html)]="model.description"
[required]="true"
[autofocus]="true"
[maxCharacters]="maxLength"
formControlName="description"
[control]="$any(form).controls['description']"
[disabled]="disabled">
[minCharacters]="minLength"
[maxCharacters]="maxLength"
[disabled]="disabled"
[required]="true">
</app-editor>
</form>
5 changes: 3 additions & 2 deletions src/app/shared/components/post-form/post-form.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { FormControl, FormGroup } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import { Translations } from 'src/app/core/services/translations.service';
import { Validators } from 'ngx-editor';

@Component({
selector: 'app-post-form',
Expand All @@ -28,7 +29,7 @@ export class PostFormComponent implements OnInit, OnDestroy {
new FormControl(
this.model.description,
[
Validators.required,
Validators.required(),
Validators.minLength(this.minLength),
Validators.maxLength(this.maxLength),
]
Expand Down

0 comments on commit 94b8d90

Please sign in to comment.