[GH-ISSUE #4] How can I get the value of form's element? #3

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

Originally created by @mostafa-asg on GitHub (Jan 10, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/4

Hi
First of all, thanks for your great work.
In the data entry form sample, How can I get the values of form?
For example in Save button, how can I get the value of first name input field?

AddButton("Save", func() {
   // I want something like this
  firstName := form.getInputField("SOME_TEXT_BOX_ID").value()
})
Originally created by @mostafa-asg on GitHub (Jan 10, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/4 Hi First of all, thanks for your great work. In the [data entry form](https://github.com/rivo/tview/wiki/Form) sample, How can I get the values of form? For example in *Save* button, how can I get the value of *first name* input field? ```Go AddButton("Save", func() { // I want something like this firstName := form.getInputField("SOME_TEXT_BOX_ID").value() }) ```
kerem closed this issue 2026-03-04 01:01:01 +03:00
Author
Owner

@rivo commented on GitHub (Jan 10, 2018):

You're right, this was missing. I don't have form element IDs so it doesn't quite work as in your example. But you now have two options:

(1) Added a change event listener to the input field in the form:

form.AddInputField("First name", "", 20, nil, func(text string) { /* handle the changed text */ })

(2) You get the field directly (by its index) and call its getter (requires a cast, though):

inputField := form.GetElement(0).(*tview.InputField)
text := inputField.GetText()
<!-- gh-comment-id:356553834 --> @rivo commented on GitHub (Jan 10, 2018): You're right, this was missing. I don't have form element IDs so it doesn't quite work as in your example. But you now have two options: (1) Added a change event listener to the input field in the form: ```go form.AddInputField("First name", "", 20, nil, func(text string) { /* handle the changed text */ }) ``` (2) You get the field directly (by its index) and call its getter (requires a cast, though): ```go inputField := form.GetElement(0).(*tview.InputField) text := inputField.GetText() ```
Author
Owner

@mostafa-asg commented on GitHub (Jan 10, 2018):

Thank you

<!-- gh-comment-id:356556488 --> @mostafa-asg commented on GitHub (Jan 10, 2018): Thank you
Author
Owner

@rivo commented on GitHub (Jan 13, 2018):

Please note that I've just committed a change which renames GetElement() to GetFormItem() for consistency reasons. I hope this won't cause too much trouble.

<!-- gh-comment-id:357430226 --> @rivo commented on GitHub (Jan 13, 2018): Please note that I've just committed a change which renames `GetElement()` to `GetFormItem()` for consistency reasons. I hope this won't cause too much trouble.
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#3
No description provided.