Skip to content

Commit

Permalink
feat: add validation fn for additionalFields
Browse files Browse the repository at this point in the history
  • Loading branch information
nzambello committed Aug 3, 2021
1 parent 62b25c2 commit 61409f0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# volto-block-form

Volto addon which adds a customizable form using a block.
Volto addon which adds a customizable form using a block.
Intended to be used with [collective.volto.formsupport](https://github.com/collective/collective.volto.formsupport).

Install with mrs-developer (see [Volto docs](https://docs.voltocms.com/customizing/add-ons/)) or with:
Expand Down Expand Up @@ -52,13 +52,24 @@ config.blocks.blocksConfig.form.additionalFields.push({
intl.formatMessage(messages.customFieldLabel) ||
'Label for field type select, translation obj or string',
component: MyCustomWidget,
isValid: (formData) => true,
});
```

The widget should have the following firm:
The widget component should have the following firm:

```js
({ id, name, title, description, required, onChange, value, isDisabled, invalid }) => ReactElement;
({
id,
name,
title,
description,
required,
onChange,
value,
isDisabled,
invalid,
}) => ReactElement;
```

## Static fields
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"repository": "https://github.com/collective/volto-form-block",
"bugs": "https://github.com/collective/volto-form-block/issues",
"author": "Nicola Zambello",
"homepage": "https://github.com/collective/volto-rss-block#readme",
"homepage": "https://github.com/collective/volto-form-block#readme",
"license": "MIT",
"keywords": [
"volto-addon",
Expand Down
3 changes: 3 additions & 0 deletions src/components/Field.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const Field = ({
isOnEdit,
valid,
disabled = false,
formHasErrors = false,
}) => {
const intl = useIntl();

Expand Down Expand Up @@ -187,6 +188,7 @@ const Field = ({
onChange={onChange}
value={value}
isDisabled={disabled}
formHasErrors={formHasErrors}
invalid={isInvalid().toString()}
{...(isInvalid() ? { className: 'is-invalid' } : {})}
/>,
Expand All @@ -211,6 +213,7 @@ Field.propTypes = {
field_type: PropTypes.string,
input_values: PropTypes.any,
value: PropTypes.any,
formHasErrors: PropTypes.bool,
onChange: PropTypes.func,
};

Expand Down
16 changes: 15 additions & 1 deletion src/components/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { submitForm } from '../actions';
import { getFieldName } from './utils';
import FormView from 'volto-form-block/components/FormView';

import config from '@plone/volto/registry';

const messages = defineMessages({
formSubmitted: {
id: 'formSubmitted',
Expand Down Expand Up @@ -92,9 +94,21 @@ const Form = ({ data, id, path }) => {
let v = [];
data.subblocks.forEach((subblock, index) => {
let name = getFieldName(subblock.label);
let additionalField =
config.blocks.blocksConfig.form.additionalFields?.filter(
(f) => f.id === name && f.isValid !== undefined,
)?.[0] ?? null;
if (
subblock.required &&
(!formData[name] || formData[name]?.length === 0)
additionalField &&
!additionalField.isValid(formData, name)
)
v.push(name);
else if (
subblock.required &&
(!formData[name] ||
formData[name]?.value?.length === 0 ||
JSON.stringify(formData[name]?.value ?? {}) === '{}')
) {
v.push(name);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/FormView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const FormView = ({
onChange={() => {}}
disabled
valid
formHasErrors={formErrors.length > 0}
/>
</Grid.Column>
</Grid.Row>
Expand Down

0 comments on commit 61409f0

Please sign in to comment.