I have a problem recently that I try to use dispatchField with the type
ADD_ROW
but when the function is execute,
const [fields, dispatchField] = useAllFormFields()
dispatchField({
type: 'ADD_ROW',
path: '__path__',
rowIndex: 0,
subFieldState: {
provider: {
initialValue: '',
value: __value__,
valid: true,
},
},
})
I think the state has been updated but there is no row being add on the dash board and also cause an error on saving draft or publishing.
[10:43:44] ERROR (payload): There was an error while creating a draft [object Object] with ID xxxxxxx.
[10:43:44] ERROR (payload): _shows_versions validation failed: version.__path__: Cast to embedded failed for value "1" (type number) at path "__path__" because of "ObjectParameterError"
[10:43:44] ERROR (payload): TypeError: Cannot read properties of undefined (reading 'version')
The workaround I do now is to manually add the row and then deleted the manually created one before saving which I think it is the additional task.
I don't know if there are any solution to solve this bug?
@iamPrompt try to use the
useFormFields
hook instead, it's much more performant and might easier to work with.
const { value } = useFormFields(([fields, dispatch]) => fields[path])
Here's some related docs, too:
https://payloadcms.com/docs/admin/hooks#useformfields@jacobsfletch I think, to get the value is fine but the way to dispatch or create a row in an array in programming way is the real problem. I'm not sure that there might be some different in
dispatch
function or not by using
useFormFields
.
I have a few fields to change, I call dispatchField once at a time, everything fine, but the array one causing an error.
As I saw in Array field-types
https://github.com/payloadcms/payload/blob/master/src/admin/components/forms/field-types/Array/index.tsx#L102when adding row in the array, there is a call to
dispatchRows
immediately after
dispatchFields
execute. I'm not sure whether
dispatchRows
is a thing, if so I have tried to find the way to access it but not successful as it might depending on its own fields. What I call is from other fields to set the value.
@iamPrompt yes, to add a row to an array you also need to update that array field's internal row state. @zubricks I think we should write a blog post about how to do this
@jmikrut is there a quick solution how to do this?
@jmikrut how do we update the array field's internal row state?
we could certainly build a utility to make this easier but right now it would take some manual work
Any idea about this??
@jmikrut any info on how to deal with this?
not yet - but we can write something up soon. in the meantime, i'd say you should look at our internal Array field for some ideas. There are two states that need to be kept up to date - the
rows
reducer, and then of course your fields themselves
Hope you will do something soon with this. I think it should be made easy to use.
Any update on this?
actually, yes, I have written up a response to someone else about how to update array fields:
https://github.com/payloadcms/payload/issues/2427
their original issue deals with nested arrays, so my example is more complex, but this should give you an idea
Thank you!
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.