feat(forms): allow minLength/maxLength validator to be bound to null
#42565
Conversation
2dbd3da
to
d1cec55
@AndrewKushnir @petebacondarwin once this PR you gives go ahead signal I will raise it similarly for other methods as well |
I might have misunderstood the plan, but I thought the idea was to implement a new |
No you are correct, let me include it. |
Thanks for the PR @iRealNirmal. I've left some comments, but as @petebacondarwin mentioned, we'd need to implement the Thank you. |
hey @AndrewKushnir @petebacondarwin when I converted it to enabled function and typescript began to give me error On the hand if I use variable directly I am not getting any error. Seems typescript is not able to infer the type narrowing, I can even do workaround but it will be overkill. Quick workaround can be like below If you have any other suggestion then please let me know. |
I wonder if we could structure this with an abstract base class? @Directive()
abstract class ValidatorBase implements OnChanges, Validator {
private _validator: ValidatorFn = nullValidator;
private _onChange?: () => void;
constructor(private _validatorKey: string) {}
ngOnChanges(changes: SimpleChanges): void {
if (this._validatorKey in changes) {
this._validator = this._enabled() ? this._createValidator() : nullValidator;
if (this._onChange) this._onChange();
}
}
validate(control: AbstractControl): ValidationErrors|null {
return this._validator(control);
}
registerOnValidatorChange(fn: () => void): void {
this._onChange = fn;
}
protected abstract _createValidator(): ValidatorFn;
protected abstract _enabled(): boolean;
} Then you could have: @Directive({
selector: '[minlength][formControlName],[minlength][formControl],[minlength][ngModel]',
providers: [MIN_LENGTH_VALIDATOR],
host: {'[attr.minlength]': '_enabled() ? minlength : null'}
})
export class MinLengthValidator implements Validator, OnChanges extends ValidatorBase {
constructor() {
super('minlength');
}
@Input() minlength!: string|number|null;
protected _createValidator(): ValidatorFn {
// This will only be called if `this.minlength` is non-null
return minLengthValidator(toInteger(this.minlength!));
}
// this may need to be public to use it in the hostbinding.
protected _enabled(): boolean {
return this.minlength !== null;
}
} |
@petebacondarwin yes good input but do you think it's fine If I can revised it to.
|
I don't really think that the |
126289d
to
5c487ed
1e45f7d
to
57cacf3
null
null
Reviewed-for: public-api Note for other public-api reviewers - the new |
6beabd0
to
b6d2fe2
Reviewed-for: public-api |
LGTM Reviewed-for: public-api |
b6d2fe2
to
f844fe7
f844fe7
to
4a5f26b
Thanks for addressing the feedback @iRealNirmal |
@iRealNirmal FYI the checks in Google's codebase went well, so I'm adding this PR to the merge queue. Thanks for contributing to Angular! |
thanks @AndrewKushnir on staying top for it, I will now will work on this kind of change for min and max validator. Let me know in case anything else. |
Thanks for the help @iRealNirmal |
If the validator is bound o be ull then no validation occurs and attribute is not added to DOM. PR already raised for minLength and maxLength valdiator (angular#42565). Changes were dicussed in angular#42378
If the validator is bound o be ull then no validation occurs and attribute is not added to DOM. PR already raised for minLength and maxLength valdiator (angular#42565). Changes were dicussed in angular#42378
If the validator is bound o be ull then no validation occurs and attribute is not added to DOM. PR already raised for minLength and maxLength valdiator (angular#42565). Changes were dicussed in angular#42378
If the validator is bound o be ull then no validation occurs and attribute is not added to DOM. PR already raised for minLength and maxLength valdiator (angular#42565). Changes were dicussed in angular#42378
If the validator is bound o be ull then no validation occurs and attribute is not added to DOM. PR already raised for minLength and maxLength valdiator (angular#42565). Changes were dicussed in angular#42378
If the validator is bound o be ull then no validation occurs and attribute is not added to DOM. PR already raised for minLength and maxLength valdiator (angular#42565). Changes were dicussed in angular#42378
If the validator is bound o be ull then no validation occurs and attribute is not added to DOM. PR already raised for minLength and maxLength valdiator (angular#42565). Changes were dicussed in angular#42378
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
If the validator is bound to be
null
then no validation occurs andattribute is not added to DOM.
For every validator type different PR will be raised as discussed in
#42378.
Closes #42267.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Null data type is not supported
Issue Number: #42267.
What is the new behavior?
Added support for null data type
Does this PR introduce a breaking change?
Other information
This is one of many PR which is going to be raised for adding support of null validation. One can go through conversation here
#42378 (comment)