Skip to content

Commit a3346e7

Browse files
committed
docs: document column value normalization
1 parent ed6bfba commit a3346e7

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,33 @@ A difference between `fillOnCreate` and `suggestOnCreate`:
652652
* `fillOnCreate` should be used when `showIn.create` is a `false` value because if it is `true`, the input will be shown in the create form but then(during actual save to db) it will be overwritten by the value returned by `fillOnCreate`.
653653
* `suggestOnCreate` should be used with `showIn.create` set to true because if it is not set, the input will not be shown in the create form and default suggestion will not make sense.
654654

655+
### Normalize values before saving
656+
657+
Use the column-level `normalize` callback when a value must always be stored in a canonical form. For example, you can trim an email address and make it lowercase:
658+
659+
```typescript title="./resources/adminuser.ts"
660+
export default {
661+
resourceId: 'adminuser',
662+
columns: [
663+
// ...
664+
{
665+
name: 'email',
666+
required: true,
667+
isUnique: true,
668+
type: AdminForthDataTypes.STRING,
669+
//diff-add
670+
normalize: (value: string) => value.trim().toLowerCase(),
671+
},
672+
],
673+
};
674+
```
675+
676+
AdminForth applies `normalize` on the backend whenever the column is present in a create or update payload. This includes records saved from the admin UI and records written with the [Data API](/docs/tutorial/Customization/dataApi). Normalization happens before backend validation and `beforeSave` hooks, so both receive the normalized value.
677+
678+
When `normalize` is configured on the column selected by `auth.usernameField`, AdminForth also normalizes the submitted username before looking up the user. The [Email Password Reset plugin](/docs/tutorial/Plugins/email-password-reset) applies the normalizer configured on its `emailField` throughout the reset flow as well. In the example above, a user saved as `admin@example.com` can therefore sign in or request a password reset with ` ADMIN@EXAMPLE.COM `.
679+
680+
`normalize` does not transform existing database records or general filter/search values. Migrate existing data first if it is not already stored in the same canonical form.
681+
655682
### Link to create form with preset values
656683

657684
Sometimes you might need to create a link that will open the create form with some fields pre-filled. For example, you might want to create a link that will open the create form with the realtor_id field pre-filled with the current user's ID.

0 commit comments

Comments
 (0)