Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array input validation #6136

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix array input validation
djhi committed Apr 12, 2021
commit 815767be5f321f48300925f0dff9f834380d7e88
55 changes: 54 additions & 1 deletion packages/ra-ui-materialui/src/input/ArrayInput.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as React from 'react';
import { render } from '@testing-library/react';
import { fireEvent, render, waitFor } from '@testing-library/react';
import { Form } from 'react-final-form';
import arrayMutators from 'final-form-arrays';

import ArrayInput from './ArrayInput';
import NumberInput from './NumberInput';
import TextInput from './TextInput';
import SimpleFormIterator from '../form/SimpleFormIterator';
import { minLength, required } from 'ra-core';

describe('<ArrayInput />', () => {
const onSubmit = jest.fn();
@@ -126,4 +127,56 @@ describe('<ArrayInput />', () => {
)
).toEqual(['bar', 'baz']);
});

it('should apply validation to both itself and its inner inputs', async () => {
const { getByText, getAllByLabelText, queryByText } = render(
<FinalForm
render={() => (
<form>
<ArrayInput
resource="bar"
source="arr"
validate={[minLength(2, 'array_min_length')]}
>
<SimpleFormIterator>
<TextInput
source="id"
validate={[required('id_required')]}
/>
<TextInput
source="foo"
validate={[required('foo_required')]}
/>
</SimpleFormIterator>
</ArrayInput>
</form>
)}
/>
);

fireEvent.click(getByText('ra.action.add'));
expect(queryByText('array_min_length')).not.toBeNull();
fireEvent.click(getByText('ra.action.add'));
const firstId = getAllByLabelText('resources.bar.fields.id *')[0];
fireEvent.change(firstId, {
target: { value: 'aaa' },
});
fireEvent.change(firstId, {
target: { value: '' },
});
fireEvent.blur(firstId);
const firstFoo = getAllByLabelText('resources.bar.fields.foo *')[0];
fireEvent.change(firstFoo, {
target: { value: 'aaa' },
});
fireEvent.change(firstFoo, {
target: { value: '' },
});
fireEvent.blur(firstFoo);
expect(queryByText('array_min_length')).toBeNull();
await waitFor(() => {
expect(queryByText('id_required')).not.toBeNull();
expect(queryByText('foo_required')).not.toBeNull();
});
});
});
32 changes: 20 additions & 12 deletions packages/ra-ui-materialui/src/input/ArrayInput.tsx
Original file line number Diff line number Diff line change
@@ -96,20 +96,21 @@ const ArrayInput: FC<ArrayInputProps> = ({
);
}

const { error, submitError, touched } = fieldProps.meta;
const { error, submitError, touched, dirty } = fieldProps.meta;
const arrayInputError = getArrayInputError(error || submitError);

return (
<FormControl
fullWidth
margin="normal"
className={className}
error={touched && !!(error || submitError)}
error={(touched || dirty) && !!arrayInputError}
{...sanitizeInputRestProps(rest)}
>
<InputLabel
htmlFor={source}
shrink
error={touched && !!(error || submitError)}
error={(touched || dirty) && !!arrayInputError}
>
<FieldTitle
label={label}
@@ -118,15 +119,6 @@ const ArrayInput: FC<ArrayInputProps> = ({
isRequired={isRequired(validate)}
/>
</InputLabel>
{!!(touched && (error || submitError)) || helperText ? (
<FormHelperText error={touched && !!(error || submitError)}>
<InputHelperText
touched={touched}
error={error || submitError}
helperText={helperText}
/>
</FormHelperText>
) : null}
{cloneElement(Children.only(children), {
...fieldProps,
record,
@@ -136,6 +128,15 @@ const ArrayInput: FC<ArrayInputProps> = ({
margin,
disabled,
})}
{!!((touched || dirty) && arrayInputError) || helperText ? (
<FormHelperText error={(touched || dirty) && !!arrayInputError}>
<InputHelperText
touched={touched || dirty}
error={arrayInputError}
helperText={helperText}
/>
</FormHelperText>
) : null}
</FormControl>
);
};
@@ -163,6 +164,13 @@ ArrayInput.defaultProps = {
fullWidth: true,
};

export const getArrayInputError = error => {
if (Array.isArray(error)) {
return undefined;
}
return error;
};

export interface ArrayInputProps extends InputProps {
children: ReactElement;
disabled?: boolean;