react-admin icon indicating copy to clipboard operation
react-admin copied to clipboard

required() doesn't mark an input required

Open Dreamsorcerer opened this issue 1 year ago • 6 comments

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.

Dreamsorcerer avatar Jan 15 '24 21:01 Dreamsorcerer

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 required attribute and the input is properly required.
  • The third input uses the validate attribute and the input is not required.
image

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.

fzaninotto avatar Jan 15 '24 23:01 fzaninotto

If we explicitly don't want browser validation, then it should have aria-required="true", right? Which should then pass the test function.

Dreamsorcerer avatar Jan 15 '24 23:01 Dreamsorcerer

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.

fzaninotto avatar Mar 12 '24 14:03 fzaninotto

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.

geobde avatar Apr 07 '24 00:04 geobde

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

endrits079 avatar May 22 '24 13:05 endrits079

That doesn't sound related to this issue..

Dreamsorcerer avatar May 22 '24 15:05 Dreamsorcerer