feat✨: let a generated page create a record keyed on a string

The generated form never showed the key column, which is right for a key
the database assigns and left a string key with nowhere to be typed: a
table keyed on one could be created through the API but not from its page.

For a string key the form now carries an input for it, required, and
disabled once the record exists - bound to form.isEdit, the same way the
hand-written dictionary and config pages lock their natural keys. A page for
a table keyed on an int renders exactly as before.

This needs go-admin-ui's useForm to record whether a form was opened to
create or to edit (go-admin-ui#310). The useForm before it inferred the mode
from the key's presence, so typing the key into a create form turned the
submit into an update.
This commit is contained in:
zhangwenjian
2026-09-25 20:21:40 +08:00
parent 719d54cf0e
commit 6e8b1891bb
+16 -1
View File
@@ -66,7 +66,7 @@
{{- if and (eq .IsList "1") (eq .FkTableName "") (eq .DictType "") (eq .HtmlType "datetime") }}{{$hasDatetime = true}}{{end -}}
{{- if eq .IsQuery "1" }}{{$hasQuery = true}}{{end -}}
{{- if and (eq .IsInsert "1") (eq .IsRequired "1") (not .Pk) (ne .GoField "CreatedAt") (ne .GoField "UpdatedAt") (ne .GoField "DeletedAt") (ne .GoField "UpdateBy") (ne .GoField "CreateBy") }}{{$hasRules = true}}{{end -}}
{{- if and .Pk (eq .GoType "string") }}{{$pkType = "string"}}{{end -}}
{{- if and .Pk (eq .GoType "string") }}{{$pkType = "string"}}{{$hasRules = true}}{{end -}}
{{- end -}}
<template>
<PageContainer>
@@ -181,6 +181,19 @@
{{- end}}
label-width="100px"
>
{{- /*
A key the database assigns never appears in the form. A string key
does not assign itself, so it is typed in on create and fixed once
the record exists - form.isEdit is true only for a form openEdit
opened, which needs go-admin-ui's useForm to record the mode rather
than infer it from the key.
*/}}
{{- if eq $pkType "string"}}
{{- $key := printf "gen.%s.%s.%s" $package $business .PkJsonField}}
<el-form-item :label="$t('{{$key}}')" prop="{{.PkJsonField}}">
<el-input v-model="form.model.{{.PkJsonField}}" :disabled="form.isEdit" />
</el-form-item>
{{- end}}
{{- range .Columns}}
{{- if and (eq .IsInsert "1") (not .Pk) (ne .GoField "CreatedAt") (ne .GoField "UpdatedAt") (ne .GoField "DeletedAt") (ne .GoField "UpdateBy") (ne .GoField "CreateBy")}}
{{- $key := printf "gen.%s.%s.%s" $package $business .JsonField}}
@@ -362,6 +375,8 @@ const { t } = useI18n()
*/}}
const rules = computed<FormRules>(() => ({ {{$rFirst := true}}
{{- if eq $pkType "string"}}{{$rFirst = false}}{{.PkJsonField}}: [{ required: true, message: t('{{printf "gen.%s.%s.%s" $package $business .PkJsonField}}'), trigger: 'blur' }]
{{- end}}
{{- range .Columns}}
{{- if and (eq .IsInsert "1") (eq .IsRequired "1") (not .Pk) (ne .GoField "CreatedAt") (ne .GoField "UpdatedAt") (ne .GoField "DeletedAt") (ne .GoField "UpdateBy") (ne .GoField "CreateBy")}}
{{- $key := printf "gen.%s.%s.%s" $package $business .JsonField}}