NewOldMax / react-form-validator-core

Core validator component for react forms
MIT License
94 stars 44 forks source link

maxStringLength issue with null value #38

Closed duykhiem closed 5 years ago

duykhiem commented 5 years ago

Hi,

In mostly "add new screen", object's properties will be null. Without input anything, the text field have null value should still satisfy all maxStringLength validation.

Current code : https://github.com/NewOldMax/react-form-validator-core/blob/master/src/ValidationRules.jsx#L55

Can we update it to something like this :

 maxStringLength: (value, length) => !isExisty (value)  || (validations.isString(value) && value.length <= length),

Thank you and Best regards, /K

NewOldMax commented 5 years ago

Hi,

when you have null value in your input and change it to something else, React will throw warning like

is changing an uncontrolled input of type text to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component

so you must define some empty value for your input in any case to avoid this, because this library assumes that all inputs are controlled.

state = {
    defaultObject: {
        title: '',
        name: '',
    }
}