Prevent default empty row being show in key-value field

Hi all The key-value field is shown with a single empty row by default. Is there a way of preventing that? I don't want any rows shown until a user adds one. Cheers
9 Replies
toeknee
toeknee15mo ago
Set them as required so the user removes them? Else, mutateBeforeSave and check for empty values and unset them
binaryfire
binaryfireOP15mo ago
But that wouldn't work for the create form, would it? The KV field in this form is optional and rarely used so I want to make it as unobtrusive as possible. The UX feels confusing when there's a field there by default. It gives the impression that something needs to be entered. It feels much better when they only appear after "Add option" is clicked
toeknee
toeknee15mo ago
So change default()
binaryfire
binaryfireOP15mo ago
Tried that. ->default(null) and ->default('') don't work unfortunately.
toeknee
toeknee15mo ago
Hmm might be a limitation of the key-value field. Just remove if if they are empty then in mutateBeforeSave
binaryfire
binaryfireOP15mo ago
Yeah I think it might be a field limitation. mutateFormDataBeforeSave won't help because I'm trying to tweak this on the create form (before anything is saved). And the empty row always shows when the field is empty/null anyway.
toeknee
toeknee14mo ago
So mutateFormDataBeforeSave should help? it will show the data and you can unset it before the save process is completed.
binaryfire
binaryfireOP14mo ago
mutateFormDataBeforeSave is only available on EditRecord. I wanted to improve the UX of the create form. But the main issue is the KV field displays an empty row when the attribute is empty/null, so mutating the data won't help.
toeknee
toeknee14mo ago
Sorry you want: mutateFormDataUsing() You will have all the data with:
->mutateFormDataUsing(function($data) {

//Get the field
$kvData = $data['my_key_value_pair_Field'];

foreach($kvData as $kvKey => $kvr) {
if(empty($kvr['value']) {
unset($kvData[$kvKey]);
}
}

$data['my_key_value_pair_Field'] = $kvData;

})
->mutateFormDataUsing(function($data) {

//Get the field
$kvData = $data['my_key_value_pair_Field'];

foreach($kvData as $kvKey => $kvr) {
if(empty($kvr['value']) {
unset($kvData[$kvKey]);
}
}

$data['my_key_value_pair_Field'] = $kvData;

})
Want results from more Discord servers?
Add your server