Thuta Learning
Angular
IntermediateWeb Developmentအလယ်အလတ်

Custom Validators

Angularသင်ခန်းစာ 2420 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Custom Validators ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
  • နမူနာ UI ကို run၊ state/output ခန့်မှန်းပြီး edge/error cases စမ်းနိုင်ရန်
  • လက်တွေ့ project တွင် typed safe pattern နဲ့ common pitfalls ကိုခွဲသိနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Custom Validators ကို ရိုးရိုးလေးနားလည်မယ်

Application ၏ specific requirements များအတွက် custom validators များ ဖန်တီးနိုင်သည်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Validator ထဲ control value ပြန်ပြင်ခြင်းက recursive updates နဲ့မခန့်မှန်းနိုင်သော state ဖြစ်စေနိုင်ပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Custom validator က value/control group ကိုယူပြီး valid ဆို null၊ invalid ဆို stable error object ပြန်သင့်တဲ့ pure function ဖြစ်ပါတယ်။ Error key နဲ့ metadata က UI message ကိုvalidator logic မှခွဲပေးပြီး factory validator က configurable rules ဖန်တီးနိုင်ပါတယ်။ Async validator တွင် request cancellation/debounce, pending state နဲ့ server error ကိုvalidation error မဟုတ်ဘဲခွဲကိုင်ပါ။

Mental Model
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

Custom Validators ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
// custom-validators.ts
import { AbstractControl, ValidationErrors } from '@angular/forms';

export class CustomValidators {
  
  // No whitespace validator
  static noWhitespace(control: AbstractControl): ValidationErrors | null {
    const isWhitespace = (control.value || '').trim().length === 0;
    return isWhitespace ? { whitespace: true } : null;
  }
  
  // Password match validator
  static passwordMatch(control: AbstractControl): ValidationErrors | null {
    const password = control.get('password');
    const confirmPassword = control.get('confirmPassword');
    
    if (!password || !confirmPassword) return null;
    
    return password.value === confirmPassword.value 
      ? null 
      : { passwordMismatch: true };
  }
}

// Usage in component
this.userForm = this.fb.group({
  username: ['', [Validators.required, CustomValidators.noWhitespace]],
  password: ['', Validators.required],
  confirmPassword: ['']
}, { validators: CustomValidators.passwordMatch });

Custom validator က value/control group ကိုယူပြီး valid ဆို null၊ invalid ဆို stable error object ပြန်သင့်တဲ့ pure function ဖြစ်ပါတယ်။ Error key နဲ့ metadata က UI message ကိုvalidator logic မှခွဲပေးပြီး factory validator က configurable rules ဖန်တီးနိုင်ပါတယ်။ Async validator တွင် request cancellation/debounce, pending state နဲ့ server error ကိုvalidation error မဟုတ်ဘဲခွဲကိုင်ပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. Input + state// custom-validators.ts

    Application ၏ specific requirements များအတွက် custom validators များ ဖန်တီးနိုင်သည်။

  2. Component + templateif (!password || !confirmPassword) return null;

    Custom validator က value/control group ကိုယူပြီး valid ဆို null၊ invalid ဆို stable error object ပြန်သင့်တဲ့ pure function ဖြစ်ပါတယ်။ Error key နဲ့ metadata က UI message ကိုvalidator logic မှခွဲပေးပြီး factory validator က configurable rules ဖန်တီးနိုင်ပါတယ်။ Async validator တွင် request cancellation/debounce, pending state နဲ့ server error ကိုvalidation error မဟုတ်ဘဲခွဲကိုင်ပါ။

  3. Rendered Angular UI}, { validators: CustomValidators.passwordMatch });

    Password match group validator နဲ့ reserved-username async validator တစ်ခုစီရေးပြီး unit tests တွင် valid, invalid, empty နဲ့ server-failure cases စမ်းပါ။

Execution Flow
  1. Input + state
  2. Component + template
  3. Rendered Angular UI

Success

Custom Validators ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

Validator ထဲ control value ပြန်ပြင်ခြင်းက recursive updates နဲ့မခန့်မှန်းနိုင်သော state ဖြစ်စေနိုင်ပါတယ်။

Application ၏ specific requirements များအတွက် custom validators များ ဖန်တီးနိုင်သည်။

typescript
// custom-validators.ts
import { AbstractControl, ValidationErrors } from '@angular/forms';

export class CustomValidators {
  
  // No whitespace validator
  static noWhitespace(control: AbstractControl): ValidationErrors | null {
    const isWhitespace = (control.value || '').trim().length === 0;
    return isWhitespace ? { whitespace: true } : null;
  }
  
  // Password match validator
  static passwordMatch(control: AbstractControl): ValidationErrors | null {
    const password = control.get('password');
    const confirmPassword = control.get('confirmPassword');
    
    if (!password || !confirmPassword) return null;
    
    return password.value === confirmPassword.value 
      ? null 
      : { passwordMismatch: true };
  }
}

// Usage in component
this.userForm = this.fb.group({
  username: ['', [Validators.required, CustomValidators.noWhitespace]],
  password: ['', Validators.required],
  confirmPassword: ['']
}, { validators: CustomValidators.passwordMatch });
You should see
(Custom validation rules များအလုပ်လုပ်ပြီး specific errors များ return ပြန်မည်)

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

Password match group validator နဲ့ reserved-username async validator တစ်ခုစီရေးပြီး unit tests တွင် valid, invalid, empty နဲ့ server-failure cases စမ်းပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Custom Validators နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
// custom-validators.ts
import { AbstractControl, ValidationErrors } from '@angular/forms';

export class CustomValidators {
  
  // No whitespace validator
  static noWhitespace(control: AbstractControl): ValidationErrors | null {
    const isWhitespace = (control.value || '').trim().length === 0;
    return isWhitespace ? { whitespace: true } : null;
  }
  
  // Password match validator
  static passwordMatch(control: AbstractControl): ValidationErrors | null {
    const password = control.get('password');
    const confirmPassword = control.get('confirmPassword');
    
    if (!password || !confirmPassword) return null;
    
    return password.value === confirmPassword.value 
      ? null 
      : { passwordMismatch: true };
  }
}

// Usage in component
this.userForm = this.fb.group({
  username: ['', [Validators.required, CustomValidators.noWhitespace]],
  password: ['', Validators.required],
  confirmPassword: ['']
}, { validators: CustomValidators.passwordMatch });

ရလဒ်: Custom Validators ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

Password match group validator နဲ့ reserved-username async validator တစ်ခုစီရေးပြီး unit tests တွင် valid, invalid, empty နဲ့ server-failure cases စမ်းပါ။

Quick Quiz 1 / 3

Custom Validators ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • Input + state → Component + template → Rendered Angular UI
  • Custom validator က value/control group ကိုယူပြီး valid ဆို null၊ invalid ဆို stable error object ပြန်သင့်တဲ့ pure function ဖြစ်ပါတယ်။ Error key နဲ့ metadata က UI message ကိုvalidator logic မှခွဲပေးပြီး factory validator က configurable rules ဖန်တီးနိုင်ပါတယ်။ Async validator တွင် request cancellation/debounce, pending state နဲ့ server error ကိုvalidation error မဟုတ်ဘဲခွဲကိုင်ပါ။
  • Validator ထဲ control value ပြန်ပြင်ခြင်းက recursive updates နဲ့မခန့်မှန်းနိုင်သော state ဖြစ်စေနိုင်ပါတယ်။
  • Password match group validator နဲ့ reserved-username async validator တစ်ခုစီရေးပြီး unit tests တွင် valid, invalid, empty နဲ့ server-failure cases စမ်းပါ။

Angular — Forms — Angular

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Validator ထဲ control value ပြန်ပြင်ခြင်းက recursive updates နဲ့မခန့်မှန်းနိုင်သော state ဖြစ်စေနိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

လက်တွေ့လေ့ကျင့်ခန်း

Custom Validators နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Custom validation rules များအလုပ်လုပ်ပြီး specific errors များ return ပြန်မည်)

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Custom Validators | Thuta Learning