react-admin
react-admin copied to clipboard
required() doesn't mark an input required
Is your feature request related to a problem? Please describe. Testing if an input is correctly required with react-testing-library fails:
const id = within(edit).getByLabelText("Id *");
expect(id).toBeRequired();
Describe the solution you'd like
In addition to adding the * to the label, I'd expect the required attribute to be set.
I can reproduce the problem in the storybook: https://react-admin-storybook.vercel.app/?path=/story/ra-ui-materialui-input-textinput--required
- The first input isn't required.
- The second input uses the
requiredattribute and the input is properlyrequired. - The third input uses the
validateattribute and the input is notrequired.
However, I'm not sure if this qualifies as a bug.
The validate prop uses JS validation, the required attribute uses browser validation. You can choose to use either one, or both.
And you can still add the attribute manually:
<TextInput source="foo" validate={required()} required />
So I'm not sure we want to fix it.
I'll wait for more feedback to move on.
If we explicitly don't want browser validation, then it should have aria-required="true", right? Which should then pass the test function.
I'm marking this as an enhancement (adding aria-required="true" to inputs using a required() validate function). You're welcome to submit a PR against next that implements this.
i would like to open an MR about this but i am not sure for the position, if i should pass the attribute as an inputProp, selectProp etc to each Input Component or somehow to pass directly to useInput hook.
Validation doesn't work either on a previously disabled field For example:
MyInputB is dependent on MyInputA value and I mark MyInputB disabled in case there is no value for InputA I have required on both fields when I try to submit the form when both inputs are empty it validates only the first input and shows that it is required, now I provide a value for inputA and inputB is no longer disabled, I submit the form it doesn't validate inputB to be required
That doesn't sound related to this issue..