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
toeknee10mo ago
Set them as required so the user removes them? Else, mutateBeforeSave and check for empty values and unset them
binaryfire
binaryfire10mo 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
toeknee10mo ago
So change default()
binaryfire
binaryfire10mo ago
Tried that. ->default(null) and ->default('') don't work unfortunately.
toeknee
toeknee10mo ago
Hmm might be a limitation of the key-value field. Just remove if if they are empty then in mutateBeforeSave
binaryfire
binaryfire10mo 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
toeknee9mo ago
So mutateFormDataBeforeSave should help? it will show the data and you can unset it before the save process is completed.
binaryfire
binaryfire9mo 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
toeknee9mo 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;

})