[GH-ISSUE #94] Feature Request: Improve method of retrieving form input #74

Closed
opened 2026-03-04 01:01:41 +03:00 by kerem · 9 comments
Owner

Originally created by @joegrasse on GitHub (Mar 30, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/94

It would be a lot easier to work with forms if you could get the input by name.

For example

firstname := form.GetFormItemByName("firstname").(*tview.InputField).GetText()
Originally created by @joegrasse on GitHub (Mar 30, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/94 It would be a lot easier to work with forms if you could get the input by name. For example ```Go firstname := form.GetFormItemByName("firstname").(*tview.InputField).GetText() ```
kerem closed this issue 2026-03-04 01:01:41 +03:00
Author
Owner

@rivo commented on GitHub (Apr 1, 2018):

I don't know. I guess. But the way it is now, I'd either have to duplicate a whole bunch of Form methods or break backwards compatibility to achieve this (i.e. by adding a name parameter to all Add methods).

I would suggest that you do this:

firstName := tview.NewInputField()
form.AddFormItem(firstName)

Then you can always reference it by its actual variable firstName and you don't even have to cast it:

name := firstName.GetText()

If it's just about getting the input field's text, I usually use the event callback to update its string variable:

form.AddInputField("First name", "", 30, nil, func(text string) {
	name = text
})

Would this help you?

<!-- gh-comment-id:377769646 --> @rivo commented on GitHub (Apr 1, 2018): I don't know. I guess. But the way it is now, I'd either have to duplicate a whole bunch of `Form` methods or break backwards compatibility to achieve this (i.e. by adding a name parameter to all `Add` methods). I would suggest that you do this: ```go firstName := tview.NewInputField() form.AddFormItem(firstName) ``` Then you can always reference it by its actual variable `firstName` and you don't even have to cast it: ```go name := firstName.GetText() ``` If it's just about getting the input field's text, I usually use the event callback to update its string variable: ```go form.AddInputField("First name", "", 30, nil, func(text string) { name = text }) ``` Would this help you?
Author
Owner

@joegrasse commented on GitHub (Apr 1, 2018):

Yes, that is what I do now in the form.AddButton("Save", func() { section. This makes it hard to break the code out that performs the save functionality though. I would have to pass all of the form items to the function, or do firstname := form.GetFormItem(0).(*tview.InputField).GetText(). As you can see, 0 mapping to firstname isn't quite obvious.

Also, I guess what I really meant was firstname := form.GetFormItemByLabel("firstname").(*tview.InputField).GetText(). This wouldn't break backwards compatibility, since the Add* methods have a label.

<!-- gh-comment-id:377793418 --> @joegrasse commented on GitHub (Apr 1, 2018): Yes, that is what I do now in the ```form.AddButton("Save", func() {``` section. This makes it hard to break the code out that performs the save functionality though. I would have to pass all of the form items to the function, or do ```firstname := form.GetFormItem(0).(*tview.InputField).GetText()```. As you can see, 0 mapping to firstname isn't quite obvious. Also, I guess what I really meant was ```firstname := form.GetFormItemByLabel("firstname").(*tview.InputField).GetText()```. This wouldn't break backwards compatibility, since the Add* methods have a label.
Author
Owner

@rivo commented on GitHub (Apr 5, 2018):

Yeah, that's what I was going to offer you. I hope I'll have this for you tomorrow at the latest.

<!-- gh-comment-id:378889988 --> @rivo commented on GitHub (Apr 5, 2018): Yeah, that's what I was going to offer you. I hope I'll have this for you tomorrow at the latest.
Author
Owner

@joegrasse commented on GitHub (Apr 5, 2018):

Awesome. Thank you.

<!-- gh-comment-id:378971013 --> @joegrasse commented on GitHub (Apr 5, 2018): Awesome. Thank you.
Author
Owner

@rivo commented on GitHub (Apr 7, 2018):

Have a look, it's there now.

<!-- gh-comment-id:379461165 --> @rivo commented on GitHub (Apr 7, 2018): Have a look, it's there now.
Author
Owner

@joegrasse commented on GitHub (Apr 8, 2018):

Thanks again.

<!-- gh-comment-id:379513192 --> @joegrasse commented on GitHub (Apr 8, 2018): Thanks again.
Author
Owner

@joegrasse commented on GitHub (Apr 9, 2018):

@rivo I have been testing out your changes and have ran into an issue. After the form has been drawn, the label can change. As-in, it can have spaces added to it to line up all of the input boxes. Here you are updating the item attributes and above you are adding spaces to the label if needed. So the label doesn't always match what you set it to, which make GetFormItemByLabel hard to use at the moment.

<!-- gh-comment-id:379773668 --> @joegrasse commented on GitHub (Apr 9, 2018): @rivo I have been testing out your changes and have ran into an issue. After the form has been drawn, the label can change. As-in, it can have spaces added to it to line up all of the input boxes. [Here](https://github.com/rivo/tview/blob/cc84c982fc1fc91616feb488f80725fdcb76c266/form.go#L323) you are updating the item attributes and [above](https://github.com/rivo/tview/blob/cc84c982fc1fc91616feb488f80725fdcb76c266/form.go#L308) you are adding spaces to the label if needed. So the label doesn't always match what you set it to, which make ```GetFormItemByLabel``` hard to use at the moment.
Author
Owner

@joegrasse commented on GitHub (Apr 9, 2018):

@rivo Created pull request to fix my issue.

<!-- gh-comment-id:379798886 --> @joegrasse commented on GitHub (Apr 9, 2018): @rivo Created pull request to fix my issue.
Author
Owner

@rivo commented on GitHub (Apr 9, 2018):

Please see my comments there.

<!-- gh-comment-id:379860568 --> @rivo commented on GitHub (Apr 9, 2018): Please see my comments there.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#74
No description provided.