Giter Club home page Giter Club logo

Comments (1)

DanRibbens avatar DanRibbens commented on June 17, 2024

I made an attempt on this, but we have complex interactions in our Forms and I couldn't wrangle this one in the time I had yesterday. I'm posting my findings as a starting point for the next attempt to fix this. I would put this effort into v3 currently the alpha branch at this point.

The function for reducing Fields to values that get sent to the server is here: packages/payload/src/admin/components/forms/Form/reduceFieldsToValues.ts

It calls a utility function called unflatten that we've imported from the flatley package. It is murdering your dot notation by making nested objects. I attempted to fix this by bypassing the unflatten conditionally depending on the field type. This doesn't work because for some reason the fields type has a conditional fieldSchema?: Field property. I don't know why fieldSchema isn't always available, but this function is called from a great many places and at this point I'm not sure if this is the best fix. It might be better to use the form overrides, but I'm not familiar enough with that to take this further.

Here is my logic that doesn't work without fieldSchema sadly:

import { unflatten as flatleyUnflatten } from 'flatley'

import type { Data, Fields } from './types'

/**
 * Reduce flattened form fields (Fields) to just map to the respective values instead of the full FormField object
 *
 * @param unflatten This also unflattens the data if `unflatten` is true. The unflattened data should match the original data structure
 * @param ignoreDisableFormData - if true, will include fields that have `disableFormData` set to true, for example, blocks or arrays fields.
 *
 */
const reduceFieldsToValues = (
  fields: Fields,
  unflatten?: boolean,
  ignoreDisableFormData?: boolean,
): Data => {
  const data = {}
  const unflattenedData = {}

  Object.keys(fields).forEach((key) => {
    if (ignoreDisableFormData === true || !fields[key].disableFormData) {
      if (unflatten && fields[key].fieldSchema?.type === 'json') {
        unflattenedData[key] = fields[key].value
      } else {
        data[key] = fields[key].value
      }
    }
  })

  if (unflatten) {
    return {...flatleyUnflatten(data, { safe: true }), ...unflattenedData}
  }

  return data
}

from payload.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.