Thuta Learning
ရှာဖွေရန်
IntermediateWeb Developmentintermediate

Custom Validators

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

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 ပြန်မည်)
Custom Validators | Thuta Learning