Open
Description
Bug Description
Adding and modifying a file with “form.File” fields in a form works correctly. However, deleting the file does not work.
How to reproduce
- Create table 'cars' in database
CREATE TABLE cars (
id INT AUTO_INCREMENT PRIMARY KEY,
model VARCHAR(255),
img VARCHAR(255)
);
- Add support table 'cars' to GoAdmin
- file
tables/tables.go
package tables
import "github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
var Generators = map[string]table.Generator{
"cars": GetCarsTable,
}
- file
tables/cars.go
package tables
import (
"github.com/GoAdminGroup/go-admin/context"
"github.com/GoAdminGroup/go-admin/modules/db"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
"github.com/GoAdminGroup/go-admin/template/types/form"
)
func GetCarsTable(ctx *context.Context) table.Table {
cars := table.NewDefaultTable(ctx, table.Config{
Driver: "mysql",
Connection: "default",
CanAdd: true,
Editable: true,
Deletable: true,
Exportable: true,
PrimaryKey: table.PrimaryKey{
Type: db.Int,
Name: "id",
},
})
info := cars.GetInfo()
info.SetTable("cars").SetTitle("List of cars").HideDetailButton()
info.AddField("ID", "id", db.Int).FieldSortable()
info.AddField("Model", "model", db.Varchar).FieldSortable()
formList := cars.GetForm()
formList.SetTable("cars").SetTitle("Car")
formList.AddField("Model", "model", db.Varchar, form.Text).FieldMust()
formList.AddField("Image", "img", db.Varchar, form.File)
return cars
}
- Go to /admin/info/cars
- Add a new car
- Set model and upload a picture on "image" field
- Save the new car
- Edit the car
- Delete the picture on "image" field.
- Save.
-> Picture always set on the "image" field - no change in database.
Capture.video.du.13-06-2025.12.28.21.webm
Expect
The database field should be emptied of the file path.
How to solve the problem
The problem is located in the plugins/admin/controller/edit.go file, around line 166:
A condition is missing in the second if.
Currently, when deleting a file, you pass through the 2 if instead of just the first -> the value of the field is therefore not stored in the database.
Actually
for i := 0; i < len(formPanel.FieldList); i++ {
if formPanel.FieldList[i].FormType == form.File &&
len(param.MultiForm.File[formPanel.FieldList[i].Field]) == 0 &&
len(param.MultiForm.Value[formPanel.FieldList[i].Field+"__delete_flag"]) > 0 &&
param.MultiForm.Value[formPanel.FieldList[i].Field+"__delete_flag"][0] != "1" {
param.MultiForm.Value[formPanel.FieldList[i].Field] = []string{""}
}
if formPanel.FieldList[i].FormType == form.File &&
len(param.MultiForm.Value[formPanel.FieldList[i].Field+"__change_flag"]) > 0 &&
param.MultiForm.Value[formPanel.FieldList[i].Field+"__change_flag"][0] != "1" {
delete(param.MultiForm.Value, formPanel.FieldList[i].Field)
}
}
With fix
for i := 0; i < len(formPanel.FieldList); i++ {
if formPanel.FieldList[i].FormType == form.File &&
len(param.MultiForm.File[formPanel.FieldList[i].Field]) == 0 &&
len(param.MultiForm.Value[formPanel.FieldList[i].Field+"__delete_flag"]) > 0 &&
param.MultiForm.Value[formPanel.FieldList[i].Field+"__delete_flag"][0] != "1" {
param.MultiForm.Value[formPanel.FieldList[i].Field] = []string{""}
}
if formPanel.FieldList[i].FormType == form.File &&
len(param.MultiForm.File[formPanel.FieldList[i].Field]) != 0 &&
len(param.MultiForm.Value[formPanel.FieldList[i].Field+"__change_flag"]) > 0 &&
param.MultiForm.Value[formPanel.FieldList[i].Field+"__change_flag"][0] != "1" {
delete(param.MultiForm.Value, formPanel.FieldList[i].Field)
}
}
Versions
- GoAdmin version: 1.2.26
- golang version: 1.23.4
- Browser: All
- OS: Linux