Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,26 @@ export default {

### minValue and maxValue

You can add `minValue` and `maxValue` limits to columns, so it will show an error below an input when entered value is out of bounds.

[Documentation in progress]
```typescript title="./resources/apartments.ts"
export default {
name: 'apartments',
columns: [
...
{
name: 'square_meter',
label: 'Square',
minValue: 3,
maxValue: 1000,
},
],
},
...
],
```

> `minValue` and `maxValue` checks are enforced both on frontend and backend.

### Validation
Expand All @@ -329,4 +347,4 @@ export default {

### Foreign resources

[Documentation in progress]
[Documentation in progress]
24 changes: 24 additions & 0 deletions adminforth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ class AdminForth implements IAdminForth {
{ resource: AdminForthResource, record: any, adminUser: AdminUser, extra?: HttpExtra }
): Promise<{ error?: string, createdRecord?: any }> {

// check if record with minValue or maxValue is within limits
for (const column of resource.columns.filter((col) => col.name in record
&& ['integer', 'decimal', 'float'].includes(col.type)
&& (col.minValue !== undefined || col.maxValue !== undefined))) {
if (column.minValue !== undefined && record[column.name] < column.minValue) {
return { error: `Value in "${column.name}" must be greater than ${column.minValue}` };
}
if (column.maxValue !== undefined && record[column.name] > column.maxValue) {
return { error: `Value in "${column.name}" must be less than ${column.maxValue}` };
}
}

// execute hook if needed
for (const hook of listify(resource.hooks?.create?.beforeSave)) {
console.log('🪲 Hook beforeSave', hook);
Expand Down Expand Up @@ -385,6 +397,18 @@ class AdminForth implements IAdminForth {
{ resource: AdminForthResource, recordId: any, record: any, oldRecord: any, adminUser: AdminUser, extra?: HttpExtra }
): Promise<{ error?: string }> {

// check if record with minValue or maxValue is within limits
for (const column of resource.columns.filter((col) => col.name in record
&& ['integer', 'decimal', 'float'].includes(col.type)
&& (col.minValue !== undefined || col.maxValue !== undefined))) {
if (column.minValue !== undefined && record[column.name] < column.minValue) {
return { error: `Value in "${column.name}" must be greater than ${column.minValue}` };
}
if (column.maxValue !== undefined && record[column.name] > column.maxValue) {
return { error: `Value in "${column.name}" must be less than ${column.maxValue}` };
}
}

// remove editReadonly columns from record
for (const column of resource.columns.filter((col) => col.editReadonly)) {
if (column.name in record)
Expand Down