'required' is needed to handle separately
See original GitHub issueI have a very simple input component.
<MyInput
required
name="phone"
validations="minLength:2"
validationError="Phone is required and must have more than 2 characters." />
The issue is when user doesn’t input any thing and submits the form, the isValid()
method will return false since the required
is not met. But getErrorMessage()
method is returning null. I have to add some additional code to handle required validation.
If I remove the required
the props, then if user submits the form without any inputs, the minLength:2
validation will be passed.
I think in the most of cases, developers will like to let null
, ''
and undefined
not meet validation of minLength:2
, then we don’t have to handle ‘required’ separately. We can change our code like
<MyInput
name="phone"
validations="minLength:2"
validationError="Phone is required and must have more than 2 characters." />
and the error message will display when user input nothing in the form.
Or is there any possible to let getErrorMessage()
method return a message when required
is not met.
The comments on the https://github.com/christianalfoni/formsy-react/issues/216 is very clear but the api is convenience enough. validations prop => showError() / getErrorMessages() required prop => showRequired()
Issue Analytics
- State:
- Created 8 years ago
- Comments:7
So, I did some digging in the implementation and it looks like when you use the
required
attribute and the field has no value the validation error messages are skipped even though they are computed. It was not clearly documented, but adding{ isDefaultRequiredValue: "Your error message when field is required" }
tovalidationErrors
will actually output that error message.See #135