Started on dynamic entity add form.
This commit is contained in:
parent
33e98f9a9e
commit
9a92c4aad6
5 changed files with 81 additions and 23 deletions
|
|
@ -153,6 +153,8 @@ func FileField(name, label string) Node {
|
|||
|
||||
func formFieldStatusClass(fm form.Form, formField string) string {
|
||||
switch {
|
||||
case fm == nil:
|
||||
return ""
|
||||
case !fm.IsSubmitted():
|
||||
return ""
|
||||
case fm.FieldHasErrors(formField):
|
||||
|
|
@ -163,6 +165,10 @@ func formFieldStatusClass(fm form.Form, formField string) string {
|
|||
}
|
||||
|
||||
func formFieldErrors(fm form.Form, field string) Node {
|
||||
if fm == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
errs := fm.GetFieldErrors(field)
|
||||
if len(errs) == 0 {
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
package pages
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"entgo.io/ent/entc/load"
|
||||
"entgo.io/ent/schema/field"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/mikestefanello/pagoda/pkg/pager"
|
||||
"github.com/mikestefanello/pagoda/pkg/ui"
|
||||
|
|
@ -31,6 +34,52 @@ func AdminEntityDelete(ctx echo.Context) error {
|
|||
return r.Render(layouts.Admin, form)
|
||||
}
|
||||
|
||||
func AdminEntityAdd(ctx echo.Context, schema *load.Schema) error {
|
||||
r := ui.NewRequest(ctx)
|
||||
r.Title = fmt.Sprintf("Add %s", "entity") // TODO
|
||||
|
||||
nodes := make(Group, 0)
|
||||
|
||||
for _, f := range schema.Fields {
|
||||
switch f.Info.Type {
|
||||
case field.TypeString:
|
||||
nodes = append(nodes, InputField(InputFieldParams{
|
||||
Name: f.Name,
|
||||
InputType: "text",
|
||||
Label: f.Name,
|
||||
}))
|
||||
case field.TypeTime:
|
||||
nodes = append(nodes, InputField(InputFieldParams{
|
||||
Name: f.Name,
|
||||
InputType: "datetime",
|
||||
Label: f.Name,
|
||||
}))
|
||||
case field.TypeBool:
|
||||
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
|
||||
default:
|
||||
nodes = append(nodes, P(Textf("%s not supported", f.Name)))
|
||||
}
|
||||
}
|
||||
|
||||
for _, e := range schema.Edges {
|
||||
if e.Inverse {
|
||||
continue
|
||||
}
|
||||
nodes = append(nodes, InputField(InputFieldParams{
|
||||
Name: e.Name,
|
||||
InputType: "number",
|
||||
Label: e.Name,
|
||||
}))
|
||||
}
|
||||
|
||||
nodes = append(nodes, ControlGroup(
|
||||
FormButton("is-primary", "Submit"),
|
||||
ButtonLink("/", "is-secondary", "Cancel"),
|
||||
), CSRF(r))
|
||||
|
||||
return r.Render(layouts.Admin, Form(Method(http.MethodPost), nodes))
|
||||
}
|
||||
|
||||
type AdminEntityListParams struct {
|
||||
Title string
|
||||
Headers []string
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue