Handle input error in custom input text

This commit is contained in:
Chocobozzz 2022-06-20 16:42:21 +02:00
parent 9bc3622320
commit 75084782b7
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
12 changed files with 49 additions and 57 deletions

View File

@ -123,13 +123,7 @@
</ng-template> </ng-template>
</my-help> </my-help>
<my-input-text <my-input-text formControlName="password" inputId="password" [formError]="formErrors['password']" autocomplete="new-password"></my-input-text>
formControlName="password" inputId="password" [ngClass]="{ 'input-error': formErrors['password'] }" autocomplete="new-password"
></my-input-text>
<div *ngIf="formErrors.password" class="form-error">
{{ formErrors.password }}
</div>
</div> </div>
<div class="form-group"> <div class="form-group">

View File

@ -1,4 +1,5 @@
<form role="form" (ngSubmit)="formValidated()" [formGroup]="form"> <form role="form" (ngSubmit)="formValidated()" [formGroup]="form">
<div class="input-group"> <div class="input-group">
<input id="password" [attr.type]="showPassword ? 'text' : 'password'" class="form-control" <input id="password" [attr.type]="showPassword ? 'text' : 'password'" class="form-control"
formControlName="password" [ngClass]="{ 'input-error': formErrors['password'] }" formControlName="password" [ngClass]="{ 'input-error': formErrors['password'] }"

View File

@ -58,12 +58,10 @@
<div class="form-group"> <div class="form-group">
<label i18n for="password">Password</label> <label i18n for="password">Password</label>
<my-input-text formControlName="password" inputId="password" <my-input-text
i18n-placeholder placeholder="Password" formControlName="password" inputId="password" i18n-placeholder placeholder="Password"
[ngClass]="{ 'input-error': formErrors['password'] }" [formError]="formErrors['password']" autocomplete="current-password" [tabindex]="2"
autocomplete="current-password" [tabindex]="2"></my-input-text> ></my-input-text>
<div *ngIf="formErrors.password" class="form-error">{{ formErrors.password }}</div>
</div> </div>
<input type="submit" class="peertube-button orange-button" i18n-value value="Login" [disabled]="!form.valid"> <input type="submit" class="peertube-button orange-button" i18n-value value="Login" [disabled]="!form.valid">

View File

@ -25,14 +25,10 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<my-input-text formControlName="password" <my-input-text
id="password" formControlName="password" id="password" i18n-placeholder placeholder="Current password"
i18n-placeholder placeholder="Current password" [formError]="formErrors['password']" autocomplete="current-password"
[ngClass]="{ 'input-error': formErrors['password'] }" ></my-input-text>
autocomplete="current-password"></my-input-text>
<div *ngIf="formErrors['password']" class="form-error">
{{ formErrors['password'] }}
</div>
</div> </div>
<input type="submit" i18n-value value="Change email" [disabled]="!form.valid"> <input type="submit" i18n-value value="Change email" [disabled]="!form.valid">

View File

@ -3,32 +3,20 @@
<form role="form" (ngSubmit)="changePassword()" [formGroup]="form"> <form role="form" (ngSubmit)="changePassword()" [formGroup]="form">
<label i18n for="current-password">Change password</label> <label i18n for="current-password">Change password</label>
<my-input-text formControlName="current-password" <my-input-text
inputId="current-password" formControlName="current-password" inputId="current-password" i18n-placeholder placeholder="Current password"
i18n-placeholder placeholder="Current password" [formError]="formErrors['current-password']" autocomplete="current-password"
[ngClass]="{ 'input-error': formErrors['current-password'] }" ></my-input-text>
autocomplete="current-password"></my-input-text>
<div *ngIf="formErrors['current-password']" class="form-error">
{{ formErrors['current-password'] }}
</div>
<my-input-text formControlName="new-password" <my-input-text
inputId="new-password" formControlName="new-password" inputId="new-password" i18n-placeholder placeholder="New password"
i18n-placeholder placeholder="New password" [formError]="formErrors['new-password']" autocomplete="new-password"
[ngClass]="{ 'input-error': formErrors['new-password'] }" ></my-input-text>
autocomplete="new-password"></my-input-text>
<div *ngIf="formErrors['new-password']" class="form-error">
{{ formErrors['new-password'] }}
</div>
<my-input-text formControlName="new-confirmed-password" <my-input-text
inputId="new-confirmed-password" formControlName="new-confirmed-password" inputId="new-confirmed-password" i18n-placeholder placeholder="Confirm new password"
i18n-placeholder placeholder="Confirm new password" [formError]="formErrors['new-confirmed-password']" autocomplete="new-password"
[ngClass]="{ 'input-error': formErrors['new-confirmed-password'] }" ></my-input-text>
autocomplete="new-password"></my-input-text>
<div *ngIf="formErrors['new-confirmed-password']" class="form-error">
{{ formErrors['new-confirmed-password'] }}
</div>
<input type="submit" i18n-value value="Change password" [disabled]="!form.valid"> <input type="submit" i18n-value value="Change password" [disabled]="!form.valid">
</form> </form>

View File

@ -7,10 +7,8 @@
<my-input-text <my-input-text
formControlName="password" inputId="password" i18n-placeholder placeholder="Password" formControlName="password" inputId="password" i18n-placeholder placeholder="Password"
[ngClass]="{ 'input-error': formErrors['password'] }" autocomplete="new-password" [formError]="formErrors['password']" autocomplete="new-password"
></my-input-text> ></my-input-text>
<div *ngIf="formErrors.password" class="form-error">{{ formErrors.password }}</div>
</div> </div>
<div class="form-group"> <div class="form-group">
@ -18,10 +16,8 @@
<my-input-text <my-input-text
formControlName="password-confirm" inputId="password-confirm" i18n-placeholder placeholder="Confirmed password" formControlName="password-confirm" inputId="password-confirm" i18n-placeholder placeholder="Confirmed password"
[ngClass]="{ 'input-error': formErrors['password-confirm'] }" autocomplete="new-password" [formError]="formErrors['password-confirm']" autocomplete="new-password"
></my-input-text> ></my-input-text>
<div *ngIf="formErrors['password-confirm']" class="form-error">{{ formErrors['password-confirm'] }}</div>
</div> </div>
<input <input

View File

@ -64,10 +64,8 @@
<my-input-text <my-input-text
formControlName="password" inputId="password" formControlName="password" inputId="password"
[ngClass]="{ 'input-error': formErrors['password'] }" autocomplete="new-password" [formError]="formErrors['password']" autocomplete="new-password"
></my-input-text> ></my-input-text>
<div *ngIf="formErrors.password" class="form-error">{{ formErrors.password }}</div>
</div> </div>
</div> </div>
</form> </form>

View File

@ -18,7 +18,7 @@
</select> </select>
</div> </div>
<my-input-text *ngIf="setting.type === 'input-password'" [formControlName]="setting.name" [inputId]="setting.name"></my-input-text> <my-input-text *ngIf="setting.type === 'input-password'" [formError]="formErrors['settings.name']" [formControlName]="setting.name" [inputId]="setting.name"></my-input-text>
<textarea *ngIf="setting.type === 'input-textarea'" type="text" [id]="setting.name" [formControlName]="setting.name"></textarea> <textarea *ngIf="setting.type === 'input-textarea'" type="text" [id]="setting.name" [formControlName]="setting.name"></textarea>
@ -40,7 +40,7 @@
<div *ngIf="setting.type === 'html'" [innerHTML]="setting.html"></div> <div *ngIf="setting.type === 'html'" [innerHTML]="setting.html"></div>
<div *ngIf="setting.type !== 'markdown-text' && setting.type !== 'markdown-enhanced' && formErrors[setting.name]" class="form-error"> <div *ngIf="hasDedicatedFormError() && formErrors[setting.name]" class="form-error">
{{ formErrors[setting.name] }} {{ formErrors[setting.name] }}
</div> </div>

View File

@ -12,4 +12,15 @@ export class DynamicFormFieldComponent {
@Input() form: FormGroup @Input() form: FormGroup
@Input() formErrors: any @Input() formErrors: any
@Input() setting: RegisterClientFormFieldOptions @Input() setting: RegisterClientFormFieldOptions
hasDedicatedFormError () {
const dedicated = new Set<RegisterClientFormFieldOptions['type']>([
'input-checkbox',
'input',
'select',
'input-textarea'
])
return dedicated.has(this.setting.type)
}
} }

View File

@ -3,6 +3,7 @@
[id]="inputId" [autocomplete]="autocomplete" [value]="value" [placeholder]="placeholder" [tabindex]="tabindex" [id]="inputId" [autocomplete]="autocomplete" [value]="value" [placeholder]="placeholder" [tabindex]="tabindex"
[(ngModel)]="value" (ngModelChange)="update()" [readonly]="readonly" [(ngModel)]="value" (ngModelChange)="update()" [readonly]="readonly"
#input (click)="input.select()" (input)="update()" (change)="update()" [type]="inputType" class="form-control" #input (click)="input.select()" (input)="update()" (change)="update()" [type]="inputType" class="form-control"
[ngClass]="{ 'input-error': formError }"
/> />
<button *ngIf="withToggle" (click)="toggle()" type="button" class="btn btn-outline-secondary" [title]="toggleTitle"> <button *ngIf="withToggle" (click)="toggle()" type="button" class="btn btn-outline-secondary" [title]="toggleTitle">
@ -18,3 +19,5 @@
<span class="copy-text">Copy</span> <span class="copy-text">Copy</span>
</button> </button>
</div> </div>
<div *ngIf="formError" class="form-error">{{ formError }}</div>

View File

@ -24,6 +24,7 @@ export class InputTextComponent implements ControlValueAccessor {
@Input() withCopy = false @Input() withCopy = false
@Input() readonly = false @Input() readonly = false
@Input() show = false @Input() show = false
@Input() formError: string
constructor (private notifier: Notifier) { } constructor (private notifier: Notifier) { }

View File

@ -77,6 +77,10 @@
} }
} }
.input-group .input-error {
z-index: 3;
}
.form-error, .form-error,
.form-warning { .form-warning {
display: block; display: block;
@ -87,7 +91,9 @@
color: $red; color: $red;
} }
.input-error:focus { // Disable red error on input focus
.input-error:focus,
.input-group:focus-within {
& + .form-error, & + .form-error,
& + * + .form-error, // Markdown textarea & + * + .form-error, // Markdown textarea
& + * + * + .form-error { & + * + * + .form-error {