Skip to content

Commit b3f0944

Browse files
committed
docs: add docs for the backend validation function
1 parent 14bdf25 commit b3f0944

File tree

1 file changed

+75
-28
lines changed

1 file changed

+75
-28
lines changed

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

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -677,34 +677,6 @@ export default {
677677
> `minValue` and `maxValue` checks are enforced both on frontend and backend.
678678
679679

680-
### Validation
681-
682-
In cases when column values must follow certain format, you can add `validation` to it.
683-
`validation` is an array of rules, each containing `regExp` that defines a format for a value and `message` that will be displayed in case when entered value does not pass the check.
684-
685-
```typescript title="./resources/adminuser.ts"
686-
export default {
687-
name: 'adminuser',
688-
columns: [
689-
...
690-
{
691-
name: 'email',
692-
required: true,
693-
isUnique: true,
694-
validation: [
695-
{
696-
regExp: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$',
697-
message: 'Email is not valid, must be in format example@test.com',
698-
},
699-
],
700-
},
701-
],
702-
},
703-
...
704-
],
705-
```
706-
707-
> `validation` checks are enforced both on frontend and backend.
708680

709681
### Input prefix and suffix
710682

@@ -967,6 +939,81 @@ When defined like this, adminforth will use value in `property_type` to figure o
967939

968940
If `beforeDatasourceRequest` or `afterDatasourceResponse` hooks are set for polymorphic foreign resource, they will be called for each resource in `polymorphicResources` array.
969941

942+
## Validation (create/edit view)
943+
In cases when column values must follow certain format, you can add `validation` to it.
944+
`validation` is an array of rules, each containing `regExp` or `validator function` that defines a format for a value and `message` that will be displayed in case when entered value does not pass the check.
945+
946+
### Frontend validation
947+
We recomend to use this validation with regExp, because it validates fields in real time
948+
949+
```typescript title="./resources/adminuser.ts"
950+
export default {
951+
name: 'adminuser',
952+
columns: [
953+
...
954+
{
955+
name: 'email',
956+
required: true,
957+
isUnique: true,
958+
//diff-add
959+
validation: [
960+
//diff-add
961+
{
962+
//diff-add
963+
regExp: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$',
964+
//diff-add
965+
message: 'Email is not valid, must be in format example@test.com',
966+
//diff-add
967+
},
968+
//diff-add
969+
],
970+
},
971+
],
972+
},
973+
...
974+
],
975+
```
976+
977+
### Backend validation
978+
There might be rare cases, when you want to have your own validation function. For this you can use `validator` callback:
979+
```typescript title="./resources/adminuser.ts"
980+
export default {
981+
name: 'adminuser',
982+
columns: [
983+
...
984+
{
985+
name: 'email',
986+
required: true,
987+
isUnique: true,
988+
//diff-add
989+
validation: [
990+
//diff-add
991+
{
992+
//diff-add
993+
async validator(value: any, record: any) {
994+
//diff-add
995+
if (value.endsWith("@example.com")) {
996+
//diff-add
997+
return { isValid: false, message: 'Your email can`t end with `@example.com`' };
998+
//diff-add
999+
}
1000+
//diff-add
1001+
return { isValid: true }
1002+
//diff-add
1003+
}
1004+
//diff-add
1005+
}
1006+
//diff-add
1007+
],
1008+
},
1009+
],
1010+
},
1011+
...
1012+
],
1013+
```
1014+
1015+
>Better avoid using of custom validator, because every time change field (after failed first save attempt), it will make an API call
1016+
9701017
## Filtering
9711018

9721019
### Filter Options

0 commit comments

Comments
 (0)