[GH-ISSUE #483] Feature Request: GetValue on FormItem #351

Closed
opened 2026-03-04 01:04:14 +03:00 by kerem · 2 comments
Owner

Originally created by @t1nfoil on GitHub (Aug 1, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/483

Hi rivo.

First off, thanks for this great library.

I have a lot of form's that I add input fields to, drop downs, etc, using the AddXXXX receiver methods (AddInputTextField, AddDropDown) etc. One thing I've noticed is that I can't seem to pull out the selected default value when a user doesn't change the fields value:

tmpForm.AddInputField("API Username", apiUserName, 256, nil, func(text string) { configStruct.ApiUsername = text })

This works well if the user changes the value in the input field, as the function will copy the text over to the configStruct.ApiUsername, however using the AddXXXX receivers, there does not appear to be a way to get the text out when referencing by label name:

`apiUsernameField := tmpForm.GetFormItemByLabel("API Username")

apiUsername := apiUsernameField.GetXXXXXXX <-- no methods to get value
`
I can get the field label "API Username" but there does not appear to be a way to get the value out of the FormItem interface?

The workaround I have found to work is to use the tview.NewInputField() method, and then assign everything to a variable, but for large forms it really adds a lot of clutter.

Originally created by @t1nfoil on GitHub (Aug 1, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/483 Hi rivo. First off, thanks for this great library. I have a lot of form's that I add input fields to, drop downs, etc, using the AddXXXX receiver methods (AddInputTextField, AddDropDown) etc. One thing I've noticed is that I can't seem to pull out the selected default value when a user doesn't change the fields value: `tmpForm.AddInputField("API Username", apiUserName, 256, nil, func(text string) { configStruct.ApiUsername = text })` This works well if the user changes the value in the input field, as the function will copy the text over to the configStruct.ApiUsername, however using the AddXXXX receivers, there does not appear to be a way to get the text out when referencing by label name: `apiUsernameField := tmpForm.GetFormItemByLabel("API Username") apiUsername := apiUsernameField.GetXXXXXXX <-- no methods to get value ` I can get the field label "API Username" but there does not appear to be a way to get the value out of the FormItem interface? The workaround I have found to work is to use the tview.NewInputField() method, and then assign everything to a variable, but for large forms it really adds a lot of clutter.
kerem closed this issue 2026-03-04 01:04:14 +03:00
Author
Owner

@rivo commented on GitHub (Sep 15, 2020):

The FormItem interface is for all form items. So the type of "value" is not the same for the different form items. (It may be a "bool" for checkboxes or a "string" for input fields.) If you want to access an input field's value directly, you'll have to do a cast:

apiUsernameField := tmpForm.GetFormItemByLabel("API Username").(*tview.InputField)
apiUsername := apiUsernameField.GetText()

Please let me know if this answers your question.

<!-- gh-comment-id:692675643 --> @rivo commented on GitHub (Sep 15, 2020): The `FormItem` interface is for all form items. So the type of "value" is not the same for the different form items. (It may be a "bool" for checkboxes or a "string" for input fields.) If you want to access an input field's value directly, you'll have to do a cast: ```go apiUsernameField := tmpForm.GetFormItemByLabel("API Username").(*tview.InputField) apiUsername := apiUsernameField.GetText() ``` Please let me know if this answers your question.
Author
Owner

@rivo commented on GitHub (Nov 17, 2020):

If you still require information, please open another issue and reference this one.

<!-- gh-comment-id:729137133 --> @rivo commented on GitHub (Nov 17, 2020): If you still require information, please open another issue and reference this one.
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#351
No description provided.