Performance: Validators (required, minLength, pattern)
Form validators affect input responsiveness and rendering updates during user interaction.
this.form = new FormGroup({ username: new FormControl('', [ Validators.required, Validators.minLength(5), Validators.pattern(/^\w+$/) ]) });
this.form = new FormGroup({ username: new FormControl('', { validators: [ () => { if (!this.form.value.username) return { required: true }; if (this.form.value.username.length < 5) return { minLength: true }; if (!/^\w+$/.test(this.form.value.username)) return { pattern: true }; return null; } ] }) });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Custom inline validators with repeated form value access | Multiple per input | Multiple per input | High due to frequent state changes | [X] Bad |
| Angular built-in validators (required, minLength, pattern) | Minimal | Single per input | Low due to optimized checks | [OK] Good |
Validators.requiredminLength checks length, pattern checks format, and maxLength limits length, but none enforce non-empty input.minLength are functions called with the required parameter in parentheses.Validators.minLength(5) correctly calls the function with 5 as argument; other options use invalid syntax.new FormControl('', [Validators.required, Validators.minLength(3)])new FormControl('', [Validators.required, Validators.pattern('[a-zA-Z]+'))Validators.required is needed.Validators.minLength(4) ensures at least 4 characters, and Validators.pattern('^[0-9]+$') ensures only digits.Pass the test and this pattern moves unknown