The before submit function allows you to run a function on the frontend before the form is submitted to the backend and optionally modify the values of a document.
import { TinaCMS, Form } from 'tinacms'type BeforeSubmitFunction = (args: {values: Record<string, unknown>cms: TinaCMSform: Form}) => Promise<void | Record<string, unknown>>
// tina/config.{ts.js}export default defineConfig({schema: {collections: [{ui: {// Example of beforeSubmitbeforeSubmit: async ({form,cms,values,}: {form: Formcms: TinaCMSvalues: Record<string, any>}) => {return {...values,lastUpdated: new Date().toISOString(),}},//...},//...},//...],},//...})
export default defineConfig({schema: {collections: [{ui: {beforeSubmit: async ({form,cms,values,}: {form: Formcms: TinaCMSvalues: Record<string, any>}) => {if (form.crudType === 'create') {return {...values,createdAt: new Date().toISOString(),}}},//...},//...},//...],},//...})
export default defineConfig({schema: {collections: [{ui: {beforeSubmit: async ({form,cms,values,}: {form: Formcms: TinaCMSvalues: Record<string, any>}) => {return {...values,slug: values.title.toLowerCase().replace(/ /g, '-').replace(/[^\w-]+/g, ''),}},//...},//...},//...],},//...})
© TinaCMS 2019–2024