B4J Tutorial [BANanoWebix] Lesson 6.X Form Validation

Discussion in 'B4J Tutorials' started by Mashiane, Jul 5, 2019.

  1. Mashiane

    Mashiane Expert Licensed User


    Let's add form validation. This is a process of ensuring that whatever is entered by users is valid.

    We accept isEmail, isNumber, isChecked, isNotEmpty.

    Let's create a form with some default settings...


    Dim form1 As WixForm
    'initialize form and make it accept names with dots (.)
    'set default settings
        form1.Form.SetDefaultLabelPosition("top")   'labels on top
        form1.Form.SetDefaultLabelWidth(140)        'fix label width
        form1.Form.SetDefaultBottomPadding(18)      'padding at bottom for msg
    We want the labels to be on top, the labelwidth to be 140 and the bottomPadding to be 18px. BottomPadding is where our hints/error messages will sit.

    We then add the various input elements needed to the form and add them to the rows of the form.

    form1.Form.CreateTextBox("").SetLabel("Username").SetRequired(True).SetBottomLabel("* spaces are allowed").SetName("user.name").SetPlaceHolder("e.g. John Smith)").SetValidateIsNotEmpty(True).SetInvalidMessage("Login cannot be empty").AddToRows(form1.Form)
    "").SetLabel("Email address").SetRequired(True).SetName("user.email").SetPlaceHolder("(e.g. john@some.com)").SetValidateIsEmail(True).SetInvalidMessage("Incorrect email address").AddToRows(form1.Form)
    "").SetLabel("Password").SetTypePassword("").SetRequired(True).SetName("user.password").SetBottomLabel("* The password must have at least 6 characters").SetValidateIsNotEmpty(True).SetInvalidMessage("Password cannot be empty").AddToRows(form1.Form)
    "").SetLabel("OTP").SetRequired(True).SetName("user.otp").SetValidateIsNumber(True).SetInvalidMessage("OTP should be a number").AddToRows(form1.Form)
    "").SetLabelRight("I accept terms of use").SetName("user.accept").SetValidateIsChecked(True).SetInvalidMessage("Must be checked").AddToRows(form1.Form)
    This includes required *, hint texts, valid parameters and invalid message settings.

    We also add a button to submit the data. On submit, the form should be validated.

    Dim cb As BANanoObject = BANano.CallBack(Me, "form_validate"Null)
    On clicking the button we want to call 'form_validate', this validates the form and provides a success / error message.

    Sub form_validate()
    Dim bv As Boolean = pg.Validate("form1")
    If bv Then
    "Invalid form!")
    End If
    End Sub
    joulongleu and Johan Schoeman like this.
  2. Mashiane

    Mashiane Expert Licensed User

    What does the .SetComplexData(true) do on the form?

    To explain, when we set the names of the controls we used, user.email, user.password etc. This has the effect of grouping your data object like.

    To get the form data (NB: all input elements must have a 'name' property) one easily calls.

    Dim fd As Map = pg.GetValues("form1")
    Great isnt it!
    joulongleu and Johan Schoeman like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice