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 ArrayInput validation #4061

Merged
merged 1 commit into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions cypress/integration/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ describe('Create Page', () => {
);
});

it('should validate ArrayInput', () => {
const backlinksContainer = cy
.get(CreatePage.elements.input('backlinks[0].date'))
.parents('.ra-input-backlinks');
backlinksContainer.contains('Remove').click();
CreatePage.submit();
backlinksContainer.contains('Required');
});

it('should have a working array input with references', () => {
CreatePage.logout();
LoginPage.login('admin', 'password');
Expand Down
1 change: 1 addition & 0 deletions examples/simple/src/posts/PostCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ const PostCreate = ({ permissions, ...props }) => {
<ArrayInput
source="backlinks"
defaultValue={backlinksDefaultValue}
validate={[required()]}
>
<SimpleFormIterator>
<DateInput source="date" />
Expand Down
6 changes: 4 additions & 2 deletions packages/ra-ui-materialui/src/form/SimpleFormIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import FormHelperText from '@material-ui/core/FormHelperText';
import { withStyles, createStyles } from '@material-ui/core/styles';
import CloseIcon from '@material-ui/icons/RemoveCircleOutline';
import AddIcon from '@material-ui/icons/AddCircleOutline';
import { translate } from 'ra-core';
import { translate, ValidationError } from 'ra-core';
import classNames from 'classnames';

import FormInput from '../form/FormInput';
Expand Down Expand Up @@ -128,7 +128,9 @@ export class SimpleFormIterator extends Component {
return fields ? (
<ul className={classes.root}>
{submitFailed && typeof error !== 'object' && error && (
<FormHelperText error>{error}</FormHelperText>
<FormHelperText error>
<ValidationError error={error} />
</FormHelperText>
)}
<TransitionGroup component={null}>
{fields.map((member, index) => (
Expand Down
8 changes: 6 additions & 2 deletions packages/ra-ui-materialui/src/input/ArrayInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { cloneElement, Children } from 'react';
import PropTypes from 'prop-types';
import { isRequired, FieldTitle } from 'ra-core';
import { isRequired, FieldTitle, composeValidators } from 'ra-core';
import { useFieldArray } from 'react-final-form-arrays';
import { InputLabel, FormControl } from '@material-ui/core';

Expand Down Expand Up @@ -60,9 +60,13 @@ export const ArrayInput = ({
margin = 'dense',
...rest
}) => {
const sanitizedValidate = Array.isArray(validate)
? composeValidators(validate)
: validate;

const fieldProps = useFieldArray(source, {
initialValue: defaultValue,
validate,
validate: sanitizedValidate,
...rest,
});

Expand Down