Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No validation for inputs of type number in firefox #78

Open
Rileran opened this issue Jul 19, 2021 · 4 comments
Open

No validation for inputs of type number in firefox #78

Rileran opened this issue Jul 19, 2021 · 4 comments
Labels

Comments

@Rileran
Copy link

Rileran commented Jul 19, 2021

Describe the bug

When using an input of type number, adding validation rules isMaxNumber or isMinNumber won't trigger validation error when the value is not a number.

Code

const MyField = (props) => {
  const { value, setValue, isValid, errorMessage } = useField(props);

  const handleChange = (e) => {
    setValue(e.target.value);
  };
  return (
    <>
      <input type="number" value={value} onChange={handleChange} />
      {!isValid && { errorMessage }}
    </>
  );
};

const Demo = () => {
  const form = useForm();
  return (
    <Formiz autoForm connect={form} onValidSubmit={console.log}>
      <MyField
        name="number"
        validations={[
          { rule: isMinNumber(0), message: 'Too low' },
          { rule: isMaxNumber(100), message: 'Too high' },
        ]}
      />
      <button onClick={() => form.submit}>Submit</button>
    </Formiz>
  );
};

Expected behavior

A validation error should trigger when not number values are used in the field.

Version

  • OS: Linux
  • Browser: Mozilla Firefox 90.0

Additional context

  • The bug won't happen in Chrome because inputs of type number are controlled by the browser to prevent bad values in the field.
  • In Firefox, the values are controlled when submitted. If the value of the field is not a number, the validation will pass, but the value returned by the form will be null.
  • In Firefox, if the value is a valid number, but too high or too low, the validation will trigger.
@ivan-dalmet
Copy link
Member

ivan-dalmet commented Jul 19, 2021

@Rileran You need to use the isNumber() validation to check if the value is number or not 😉

@Rileran
Copy link
Author

Rileran commented Jul 19, 2021

Then there is a problem within the documentation.

isMinNumber(min)
Tests if the value is a number and above a min value.

Sending a PR to fix the doc !

@ivan-dalmet
Copy link
Member

@Rileran Wait, I answered too fast 😔
The documentation is right, and it should not be valid. I see the issue on Firefox, the value is not set if it's not a number.

@ivan-dalmet
Copy link
Member

ivan-dalmet commented Jul 19, 2021

You can fix this issue by replacing type="number" by type="tel". It will work the same for mobile keyboards and it will work as expected in firefox.

@ivan-dalmet ivan-dalmet added the v1 label Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants