Dealing with user input

Fields define what makes for valid user input. Link an Input Widget to a Field, and it will automatically validate user input in JavaScript as well as server-side (in case your JavaScript is bypassed).

Once you define a Field on an object, you can re-use it from different Inputs.

Try it out

  • Type 'something' into the input below and tab out: notice the validation errors.
  • Now, edit it to be valid, or remove it.

class Comment:
    fields = ExposedNames()
    fields.email_address = lambda i: EmailField(label='Email address', required=True)
    
    events = ExposedNames()
    events.do_nothing = lambda i: Event(label='Submit')

class CommentForm(Form):
    def __init__(self, view):
        super().__init__(view, 'myform')

        new_comment = Comment()
        self.use_layout(FormLayout())
        self.layout.add_input( TextInput(self, new_comment.fields.email_address) )

        self.define_event_handler(new_comment.events.do_nothing)
        self.add_child(ButtonInput(self, new_comment.events.do_nothing))