Skip to content

Commit 7089d26

Browse files
authored
Merge pull request #59 from devforth/min-max-backend-check
fix: add minValue and maxValue check on backend
2 parents 304accc + 364c5ca commit 7089d26

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

adminforth/documentation/docs/tutorial/03-Customization/13-standardPagesTuning.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,26 @@ export default {
319319

320320
### minValue and maxValue
321321

322+
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.
322323

323-
[Documentation in progress]
324+
```typescript title="./resources/apartments.ts"
325+
export default {
326+
name: 'apartments',
327+
columns: [
328+
...
329+
{
330+
name: 'square_meter',
331+
label: 'Square',
332+
minValue: 3,
333+
maxValue: 1000,
334+
},
335+
],
336+
},
337+
...
338+
],
339+
```
340+
341+
> `minValue` and `maxValue` checks are enforced both on frontend and backend.
324342
325343

326344
### Validation
@@ -329,4 +347,4 @@ export default {
329347

330348
### Foreign resources
331349

332-
[Documentation in progress]
350+
[Documentation in progress]

adminforth/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,18 @@ class AdminForth implements IAdminForth {
320320
{ resource: AdminForthResource, record: any, adminUser: AdminUser, extra?: HttpExtra }
321321
): Promise<{ error?: string, createdRecord?: any }> {
322322

323+
// check if record with minValue or maxValue is within limits
324+
for (const column of resource.columns.filter((col) => col.name in record
325+
&& ['integer', 'decimal', 'float'].includes(col.type)
326+
&& (col.minValue !== undefined || col.maxValue !== undefined))) {
327+
if (column.minValue !== undefined && record[column.name] < column.minValue) {
328+
return { error: `Value in "${column.name}" must be greater than ${column.minValue}` };
329+
}
330+
if (column.maxValue !== undefined && record[column.name] > column.maxValue) {
331+
return { error: `Value in "${column.name}" must be less than ${column.maxValue}` };
332+
}
333+
}
334+
323335
// execute hook if needed
324336
for (const hook of listify(resource.hooks?.create?.beforeSave)) {
325337
console.log('🪲 Hook beforeSave', hook);
@@ -385,6 +397,18 @@ class AdminForth implements IAdminForth {
385397
{ resource: AdminForthResource, recordId: any, record: any, oldRecord: any, adminUser: AdminUser, extra?: HttpExtra }
386398
): Promise<{ error?: string }> {
387399

400+
// check if record with minValue or maxValue is within limits
401+
for (const column of resource.columns.filter((col) => col.name in record
402+
&& ['integer', 'decimal', 'float'].includes(col.type)
403+
&& (col.minValue !== undefined || col.maxValue !== undefined))) {
404+
if (column.minValue !== undefined && record[column.name] < column.minValue) {
405+
return { error: `Value in "${column.name}" must be greater than ${column.minValue}` };
406+
}
407+
if (column.maxValue !== undefined && record[column.name] > column.maxValue) {
408+
return { error: `Value in "${column.name}" must be less than ${column.maxValue}` };
409+
}
410+
}
411+
388412
// remove editReadonly columns from record
389413
for (const column of resource.columns.filter((col) => col.editReadonly)) {
390414
if (column.name in record)

0 commit comments

Comments
 (0)