Skip to content

Commit

Permalink
feat: enhance form handling and data integrity in admin interface
Browse files Browse the repository at this point in the history
- Exclude ID field from edit forms to prevent accidental changes
- Update form action URL for editing instances, ensuring proper routing
- Ensure primary key is included in update data, enhancing data integrity
  • Loading branch information
infuzu-yidisprei committed Oct 3, 2024
1 parent ed90a2b commit 37a00c5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type TestModel1 struct {
ID uint `gorm:"primarykey"`
ID uint `gorm:"primarykey" admin:"editForm:exclude"`
Name string
}

Expand Down
2 changes: 1 addition & 1 deletion internal/templates/edit_instance.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h2>Models Sidepanel</h2>
{{ end }}
</ul>
<h2>Edit {{ .model.DisplayName }}</h2>
<form method="post" action="{{ .model.GetFullAddLink }}">
<form method="post" action="{{ .model.GetFullLink }}/{{ .form.InstanceID }}/edit">
{{ formAsP .form .formErrs .fieldErrs }}
<button type="submit">Submit</button>
</form>
Expand Down
4 changes: 4 additions & 0 deletions orm-integrations/gorm/integrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ func (i *Integrator) UpdateInstanceOnlyFields(instance interface{}, fields []str
}
updateData[fieldName] = fieldValue.Interface()
}
if _, exists := updateData[primaryField.Name]; !exists {
updateData[primaryField.Name] = primaryKey
fields = append(fields, primaryField.Name)
}

return i.DB.Model(instance).Where(fmt.Sprintf("%s = ?", primaryKeyDBName), primaryKey).Select(fields).Updates(updateData).Error
}

0 comments on commit 37a00c5

Please sign in to comment.