Accessing relationship data in form lifecycle hook
I'm trying to accessing (and possibly unsetting) a form input in the mutateDataBeforeCreate() hook. The form input is in a relationship (via Group::make()->relationship). But accessing the $data variable only shows the inputs from the root resource, all the relationship-related inputs are not there. How can I access them?
8 Replies
Relation data is saved directly by the fields and therefore not included. If you want to modify that data, there are similar methods on those fileds I think
Mh... the mutateDataBeforeCreate and handleRecordCreation only take $data as argument. Do you mean accessing the form field's data through an entire different method?
Perhaps my approach is not right overall. I have a hasMany relationship and would like to enable the upload of a file via a Document model (of which the main model can have several in total) in a form.
Since I would like to have the field where it is now (in the relationship group) and relationship() does not support hasMany anyway, my idea was to put the upload field there, but remove it before saving and create the content manually in the Document model and then saving the main model with the rest of the fields. But right now I can't seem to access the form fields data of the relationship
Yes. They are on that field (not the page)
So the only reason why you place the field there is for design reasons?
Basically, yes. If I place it outside the relationship, I should be able to access it via $data.
Yes
Can't you get the same/similar look with the field outside? š¤
Here is the form part I'm working on:
There is a dropdown in the left part of the grid and dependent on the selected option I have a second select on the right or this File Upload
Moving it outside of the relationship group seems to break the hidden() conditional
Ah I have to reference the relationship in the hidden() method as well
you would need to travese with ../ or trafficRegulationOrder/traffic_regulation to get the value
>hidden(fn(Get $get) => $get('trafficRegulationOrder.traffic_regulation') !== 'beigefĆ¼gtem Regelplan') works, layout wise I have to rethink my initial plan