Validate the avatar upload

Angular · Angular · intermediate · greenfield

Adds the standalone `AvatarUploadComponent`: validates the picked file's MIME type and size, shows a specific `role="alert"` error for each failure mode, and emits valid files through a signal output. Resets the input value after every pick so re-selecting the same file works. Tried a GIF, an 8 MB JPEG, and a 5 MB-on-the-nose PNG.

Requirements

Files touched

--- src/app/profile/avatar-upload.component.ts
+import { Component, output, signal } from '@angular/core';
+
+const MAX_BYTES = 5 * 1024 * 1024;
+const ALLOWED_TYPES = ['image/png', 'image/jpeg'];
+
+@Component({
+  selector: 'app-avatar-upload',
+  standalone: true,
+  template: `
+    <label>
+      Avatar (PNG or JPEG, max 5 MB)
+      <input type="file" accept="image/png,image/jpeg" (change)="onPick($event)" />
+    </label>
+    @if (error(); as message) {
+      <p class="error" role="alert">{{ message }}</p>
+    }
+  `,
+  styles: `
+    .error {
+      color: var(--danger-fg);
+    }
+  `,
+})
+export class AvatarUploadComponent {
+  readonly fileSelected = output<File>();
+
+  protected readonly error = signal<string | null>(null);
+
+  protected onPick(event: Event): void {

Review this PR

Angular practice