Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prevent binding of a particular struct field? #1631

Closed
singh-gursharan opened this issue Sep 4, 2020 · 3 comments
Closed

How to prevent binding of a particular struct field? #1631

singh-gursharan opened this issue Sep 4, 2020 · 3 comments

Comments

@singh-gursharan
Copy link

how can we prevent bind function to bind some form fields to the struct fields?
For example
type student struct{
ID int form:"-"
Name string form:"name"
Details string form:"details"
}

Here i do not want bind function in echo to bind id from the create student request to ID. but if i post like. -:1 name:student details:student

it will bind - value to ID which i do not want.

And if i skip form tag as shown below
type student struct{
ID int
Name string form:"name"
Details string form:"details"
}

then do a post request

id:1 name:student details:student

now the bind function will bind id with ID.

the problem is I am not able to skip binding for ID field

@BogdanDarius
Copy link

Just dont include the ID inside the type.
If you want to include the ID a simple workaround would be to create a temporary student ts := student{} bind data to the temporary student and then make your student type s := student{} and copy only the fields you want s.Name = ts.Name and s.Details = ts.Details

@pafuent
Copy link
Contributor

pafuent commented Nov 7, 2020

Please see my comment
It contains some examples to show how binding works on Echo. Also at the end there is a promise (😉) of a new PR adding a configuration to the default Binder to turn on/off the automatic binding by struct field name. Please let me know if that could be useful to you and I'll try to come up with something.

pafuent added a commit to pafuent/echo that referenced this issue Nov 10, 2020
Now the DefaultBinder could be configured to avoid binding struct fields
by name. This is particularly useful when the user don't want to bind
certain struct fields (with this config in true, only the tagged fields
will be binded)

Fixes labstack#1620, fixes labstack#1631, partially fixes labstack#1670
@lammel
Copy link
Contributor

lammel commented Mar 9, 2021

No more comments from the reporter of the ticket. Closing.

@lammel lammel closed this as completed Mar 9, 2021
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 a pull request may close this issue.

4 participants