Skip to content

required validator does not work with number #6932

@fredgate

Description

@fredgate

The required validator does not work with a input whose type is number :
<input type="number" ngControl="weight" />

this.form = fb.group({
  weight: ["", Validators.required]
});

When the input is empty, no error appear.

The reason is that the NumberValueAccessor selector match the input, and set the value of the Control to a number. If the entered string of the input does not match a number, the value is NaN. This is the case for the empty string.
But the required validator return a error only if the control value is blank (null or undefined) or is a empty string. This is not the case of any of these two conditions because the NumberValueAccessor set the value to the NaN number.

The required validator can not really verify the NaN value because it can appear for empty string, but also for string that are not a number (like 'aaa'). The problem is that the validator can not retrieve the entered string from the control variable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions