Skip to content

Commit

Permalink
Fix/checkbox validation error position (#521)
Browse files Browse the repository at this point in the history
* fix: checkbox validation error positioning

* feat: sanitize defaultValue to false when field is required
  • Loading branch information
DanRibbens committed Apr 12, 2022
1 parent a4f2c5a commit 6f84c0a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions demo/collections/RelationshipB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const RelationshipB: CollectionConfig = {
{
name: 'disableRelation', // used on RelationshipA.filterRelationship field
type: 'checkbox',
required: true,
admin: {
position: 'sidebar',
},
Expand Down
2 changes: 1 addition & 1 deletion docs/fields/checkbox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ keywords: checkbox, fields, config, configuration, documentation, Content Manage
| **`hooks`** | Provide field-based hooks to control logic for this field. [More](/docs/fields/overview#field-level-hooks) |
| **`access`** | Provide field-based access control to denote what users can see and do with this field's data. [More](/docs/fields/overview#field-level-access-control) |
| **`hidden`** | Restrict this field's visibility from all APIs entirely. Will still be saved to the database, but will not appear in any API or the Admin panel. |
| **`defaultValue`** | Provide data to be used for this field's default value. |
| **`defaultValue`** | Provide data to be used for this field's default value, will default to false if field is also `required`. |
| **`localized`** | Enable localization for this field. Requires [localization to be enabled](/docs/configuration/localization) in the Base config. |
| **`required`** | Require this field to have a value. |
| **`admin`** | Admin-specific configuration. See the [default field admin config](/docs/fields/overview#admin-config) for more details. |
Expand Down
3 changes: 3 additions & 0 deletions src/admin/components/forms/field-types/Checkbox/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

.tooltip {
right: auto;
position: relative;
margin-bottom: .2em;
max-width: fit-content;
}

&__error-wrap {
Expand Down
4 changes: 4 additions & 0 deletions src/fields/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const sanitizeFields = (fields, validRelationships: string[]) => {
field.label = toWords(field.name);
}

if (field.type === 'checkbox' && typeof field.defaulValue === 'undefined' && field.required === true) {
field.defaultValue = false;
}

if (field.type === 'relationship') {
const relationships = Array.isArray(field.relationTo) ? field.relationTo : [field.relationTo];
relationships.forEach((relationship: string) => {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import pino from 'pino';
import crypto from 'crypto';
import {
TypeWithID,
Collection, CollectionModel,
Collection,
CollectionModel,
} from './collections/config/types';
import {
SanitizedConfig,
Expand Down

0 comments on commit 6f84c0a

Please sign in to comment.