marmelab / react-admin

A frontend Framework for single-page applications on top of REST/GraphQL APIs, using TypeScript, React and Material Design
http://marmelab.com/react-admin
MIT License
24.73k stars 5.21k forks source link

Initially disabled inputs are not validated after enabled when form is submitted #9870

Closed endrits079 closed 1 week ago

endrits079 commented 3 months ago

What you were expecting:

I have a form with 2 inputs, both inputs are required but the second is disabled if there is no value for the first input, after I provide a value for the first input the second input becomes enabled but if I submit the form without providing a value for second input the form submission should failed because the required() should fail, What happened instead:

The form was submitted and ignored the fact that there is no value on a required() validation input. Steps to reproduce:

1.Go to create post, Fill in the required fields but leave the average_note blank, it is required() and minimum 1 but it can submit the form without providing value for it

Related code:

adguernier commented 3 months ago

Reproduced, thanks. This issue seems to be related to #9753.

For now, you can use the validate prop of the <Form> component to apply the validation you need.

fzaninotto commented 3 months ago

Indeed, if you replace disabled by readOnly, the problem disappears. I suspect this is a react-hook-form bug.

endrits079 commented 3 months ago

Indeed, if you replace disabled by readOnly, the problem disappears. I suspect this is a react-hook-form bug.

I didn't check in details how are you using react hook form but I tried a basic example with react hook only and it was working fine

slax57 commented 3 months ago

@endrits079 we had issues in the past with a breaking change introduced by RHF in version 7.47.0 which affected disabled inputs. That's why we made sure the readOnly prop is supported on every input component in react-admin (https://github.com/marmelab/react-admin/pull/9656). If using readOnly allows to fix the problem, we will consider this issue solved. If using readOnly cannot be applied in your case, feel free to elaborate.

fzaninotto commented 1 month ago

@endrits079 Any updates?

endrits079 commented 1 month ago

@endrits079 Any updates?

using readonly indeed solved the issue, but introduced a new issue, because it was validating readonly fields so I had to change the validation logic to ignore readonly fields

slax57 commented 1 week ago

Fixed via https://github.com/marmelab/react-admin/pull/10163