Skip to content

Conversation

@vldmr-k
Copy link

@vldmr-k vldmr-k commented Oct 12, 2022

I think, set custom attrs in json format is friendly and shortest way.

If you don't think so, let me know

@joncalhoun
Copy link
Owner

I have mixed feelings about this PR. On one hand I get that attributes can be handy with some JS libraries, but on the other hand this was meant to be a fairly simplistic library and this starts to complicate things.

One thing I can say with certainty is that I don't care for the use of JSON in the struct tag. eg:

`form:"attrs={\"ts-action\":\"class+ loading, 'parent .parent'\", \"ts-trigger\": \"click\"}"`

I get why you used it, but I think that format is awful to read and will be problematic long term.

If allowing for attributes like these, I suspect a better approach might be to allow struct tags of the format attr-<name> and updating the code in reflect.go to look for any struct tags with that prefix and to assemble an attribute that way. Eg:

`form:"attr-ts-action:class+ loading;attr-ts-trigger:click"`

I suspect this still might lead to edge cases that don't work well, but at the very least it follows an approach similar to all of the other HTML attributes.

Alternatively, I might see an argument for just exposing all extra struct tags to the HTML template as a map of key/values. In that case the attr- prefix wouldn't be needed, as a struct tag like the following:

`form:"ts-action:class+ loading;ts-trigger:click"`

Would potentially allow for HTML roughly like:

{{range $key, $value := .ExtraStructTags}} {{$key}}="{{$value}}"{{end}}

This would also eliminate the need for anymore PRs like this for random edge cases, as you could also use the index function provided by templates to get a specific struct tag value, so it is appealing to just eliminate all random edge cases at once.

@vldmr-k
Copy link
Author

vldmr-k commented Oct 13, 2022

I think, I got better idea

Your lib doesn't has layout of form, only for input.
How to resolve form in two columns, or first line one column, second line - two columns, html code, third line one column.

In this case, my proposal add layout of form.

Default mode
we can use InputTemplate and render form as {{ inputs_for . }}

Layout mode
we generate template functions by each struct field, and we can build form manually

For example

type Member struct {
    Id int
    Name string
}

In layout form we generate next function
{{fieldName}}_row - render input using template from InputTemplate

Also we can build own input widget
{{fieldname}}_label - input's label
{{fieldname}}_errors - input's errors
{{fieldname}}_widget - render input and in this function we can pass arguments like as custom css classes, attributes etc.
{{fielname}}_footer - input's footer attributes

So, Want to hear you opinion? What do you think about it?
In this way, we ignore add new tags in struct field, and keep small. But if we need more feature, we can use layout mode and build form manually

PS: this idea resolve in symfony form.

@joncalhoun
Copy link
Owner

joncalhoun commented Oct 13, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants