Form data in examples #5725
eddyizm
started this conversation in
Ideas / Feature Requests
Replies: 1 comment 4 replies
-
|
Thanks for the feedback, @eddyizm! You're right that we don't have a dedicated "form" example in the docs. NiceGUI takes a different approach than traditional HTML forms — instead of a Here are the key patterns: Simple form with direct value access: from nicegui import ui
name = ui.input('Name')
email = ui.input('Email')
def submit():
print(f'{name.value}, {email.value}')
ui.notify('Submitted!')
ui.button('Submit', on_click=submit)
ui.run()Using data binding (recommended for larger forms): from nicegui import ui
from dataclasses import dataclass
@dataclass
class FormData:
name: str = ''
email: str = ''
age: int = 0
data = FormData()
ui.input('Name').bind_value(data, 'name')
ui.input('Email').bind_value(data, 'email')
ui.number('Age').bind_value(data, 'age')
def submit():
print(data) # FormData(name='...', email='...', age=...)
ui.notify('Submitted!')
ui.button('Submit', on_click=submit)
ui.run()With validation: from nicegui import ui
name = ui.input('Name', validation={'Required': lambda v: len(v) > 0})
email = ui.input('Email', validation={'Invalid email': lambda v: '@' in v})
def submit():
if all(field.validate() for field in [name, email]):
ui.notify('Valid!', type='positive')
else:
ui.notify('Please fix errors', type='negative')
ui.button('Submit', on_click=submit)
ui.run()Some helpful doc pages: That said, having a dedicated "form" example combining these patterns in one place is a good idea — we'll keep it in mind for the docs. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have been pouring over the docs as I build out a native app. I am noticing though that I don't see any example of a regular vanilla form, much less a complex one.
Searched the discord/reddit and these issues and came across this discussion. That would be good to have in the docs unless I have missed it in the extensive documentation in which case, please feel free to point me to them.
Beta Was this translation helpful? Give feedback.
All reactions