[GH-ISSUE #931] how can I can use color or style to warn that input is invalid #680

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

Originally created by @stalwartgiraffe on GitHub (Dec 30, 2023).
Original GitHub issue: https://github.com/rivo/tview/issues/931

The Form InputField has an Acceptance Function where if the input string is invalid the accept handler can returns false and the users key press is ignored.

Rather than ignoring a key press, I would rather allow the input text to be incorrect but give the user a visual indicator that the input string is considered invalid. For example, turn the input text field red to signal an error.
But I am not seeing the expected color change when I try to set the field color update in the event handler...

	const Description = "Description"
	isEven:= false // simulate bad input with even odd
	form.AddInputField(Description, "", 60,
		func(textToCheck string, lastChar rune) bool {
			field := form.GetFormItemByLabel(Description).(*tview.InputField)
			isEven = !isEven // imp
			if isEven {
				field.SetFieldBackgroundColor(tcell.ColorPink)
			} else {
				field.SetFieldBackgroundColor(tcell.ColorBlue)
			}
			return true
		},
		nil,
	)
Originally created by @stalwartgiraffe on GitHub (Dec 30, 2023). Original GitHub issue: https://github.com/rivo/tview/issues/931 The Form InputField has an Acceptance Function where if the input string is invalid the accept handler can returns false and the users key press is ignored. Rather than ignoring a key press, I would rather allow the input text to be incorrect but give the user a visual indicator that the input string is considered invalid. For example, turn the input text field red to signal an error. But I am not seeing the expected color change when I try to set the field color update in the event handler... ``` const Description = "Description" isEven:= false // simulate bad input with even odd form.AddInputField(Description, "", 60, func(textToCheck string, lastChar rune) bool { field := form.GetFormItemByLabel(Description).(*tview.InputField) isEven = !isEven // imp if isEven { field.SetFieldBackgroundColor(tcell.ColorPink) } else { field.SetFieldBackgroundColor(tcell.ColorBlue) } return true }, nil, ) ```
kerem closed this issue 2026-03-04 01:07:00 +03:00
Author
Owner

@stalwartgiraffe commented on GitHub (Dec 30, 2023):

Ah. I based my initial code on the basic Form demo, but the AddFormItem documentation at
https://pkg.go.dev/github.com/rivo/tview#Form
says
" Note, however, that the Form class will override some of its attributes to make it work in the form context."

So ok... making custom items style in this manner appears unsupported with the default Form.

ok I guess the new question is there a good tutorial on making a custom form with custom items? How much work is it to do that?

<!-- gh-comment-id:1872545300 --> @stalwartgiraffe commented on GitHub (Dec 30, 2023): Ah. I based my initial code on the basic Form demo, but the AddFormItem documentation at https://pkg.go.dev/github.com/rivo/tview#Form says " Note, however, that the Form class will override some of its attributes to make it work in the form context." So ok... making custom items style in this manner appears unsupported with the default Form. ok I guess the new question is there a good tutorial on making a custom form with custom items? How much work is it to do that?
Author
Owner

@stalwartgiraffe commented on GitHub (Jan 1, 2024):

I see. SetAttributes is called from the Form Draw() loop, which iterates the collection of items and buttons doing layout and then draws each one.

Thank you

<!-- gh-comment-id:1873336030 --> @stalwartgiraffe commented on GitHub (Jan 1, 2024): I see. SetAttributes is called from the Form Draw() loop, which iterates the collection of items and buttons doing layout and then draws each one. Thank you
Author
Owner

@stalwartgiraffe commented on GitHub (Jan 2, 2024):

Ok Thank you

<!-- gh-comment-id:1873885881 --> @stalwartgiraffe commented on GitHub (Jan 2, 2024): Ok Thank you
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#680
No description provided.