Tutorial 20 - How to add validation function in a form

What you will learn in this Tutorial

How to write business logic in native SDK using action FUNCTION

Download Source Code - Tutorials - Source for this tutorial is in appemble-tutorial-validation-fn
Reference Action - FUNCTION, Data Sourcing

Introduction

One of the common requirements in a screen that takes user input is to validate user inputs. In this tutorial you are going to learn about writing business logic in native SDK’s language and do user input validation. This is done using action FUNCTION.

FUNCTION plays a central role in the AVM action system. It is the way to extend the functionality of AVM. FUNCTIONS are written in SDK’s native language and if you understand the AVM event system, you can pretty must do everything that you could do with an app that is completely written in native SDK. You can also read more about the action FUNCTION to learn about

  • The role of this action in AVM.
  • How to attach the action to a event using XMADL?
  • How to define the implementation of the action in native SDK’s language?

How to add a function

In order to add business logic, the following needs to be done.

Step 1 - See How to add functions in the project?

Step 2

  • Android - Write the function in Java
  • iOS - Write the function in Objective C

Step 3 - Add action_name=”FUNCTION” target=”<name of function>”

<action event_list="TAP" action_name="FUNCTION"
        target="<name of function>"
        input_parameter_list="..........">

In this tutorial the FUNCTION InputValidation has been added.

<action event_list="TAP" action_name="FUNCTION"
        target="InputValidation"
        input_parameter_list="lastname, firstname, email, password">

The validation rules in the InputValidation function are as follows

  • The firstname and lastname cannot be blank or more than 30 characters.
  • The email cannot be blank and must confirm to the email syntax.
  • The password cannot be blank or less than 8 characters.

See the InputValidation function for more details.

Android

See appemble-tutorial-validation-fn/src/com.appemble.tutorial.validationfunction.functions/InputValidation.java. The class file defines a function execute(). It is called by AVM while executing the action.

The function execute() receives the user inputs in the variable Bundle targetParameterValueList in the form of key/value pairs. The key in the key/value pair is the field_name and the value is the user input. The list of field name(s) to be sent is specified using the action attribute input_parameter_list (or target_parameter_list if different from input_parameter_list). In the code below, the action definition is passing the field names lastname, firstname, email and password.

In the function execute(), the user input values are retreived from targetParameterValueList and validation rules are applied in the code.

If any of the above conditions fails, an error message is displayed to the user and the cascading actions are not executed (EXECUTE_SQL and CLOSE_SCREEN).


Test the appemble-tutorial-validation-fn. If you have not downloaded tutorial source code, click on Download Source Code - Tutorials

Android - Right click on appemble-tutorial-validation-fn and click on Run as Android application.

Android
../_images/val_fn1.png ../_images/val_fn2.png

XCode

See appemble-tutorial-validation-fn/Project/Application/Source/InputValidation.h and InputValidation.m

In the function executeWithActionModel(), the user input values are retreived from targetParameterValueList and validation rules are applied in the code.

If any of the above conditions fails, an error message is displayed to the user and the cascading actions are not executed (EXECUTE_SQL and CLOSE_SCREEN).


Test the appemble-tutorial-validation-fn. If you have not downloaded tutorial source code, click on Download Source Code - Tutorials

iOS - Select scheme appemble-tutorial-validation-fn and iPhone/iPad x.x simulator. Click on Product -> Run.

iOS
../_images/ios_val_fn1.png ../_images/ios_val_fn2.png