In this chapter we will add validation to the data users type into forms. To do this, we need to add custom actions to insert and update actions. Adding custom actions is an important steps, and allows you to do many things with actions beyond validating data. When adding custom actions to update and insert actions, there are a couple of crucial things to remember:
To recap the steps in the video:
- Add a new attribute to the stream
Contacts
:BusinessName
- Drag this into both the top grid view and the bottom edit form of the dashboard
Contacts
- Now add an action to validate the data typed in for both new contacts, and contact edits:
- Open the configuration form for the stream
Contacts
- Go to the section Actions, and add a new action
- Give the action the following settings:
- Name:
Validate Contact
- Display Name:
Validate Contact
- Go to the section Update StreamItems
- Set Stream Item Action to Update
- Press Apply
- Tick Copy Values by Name
- Name:
- Open the configuration form for the stream
Copy Values by Name updates the underlying stream with values types into a form if the names of the fields match. All standard actions will do this. When you add a custom action to update or insert, it is important that you tick this, otherwise the form will not continue to work as it currently does, and you will not be able to easily as new fields as you have done so far.
In future exercises we will look at cases where you might not want this flag ticked, and cases where the flag is ticked, but for particular fields you can override the copy to set the value to something else.
- Go to the section Action Validation
- Type in the following expression, to check that phone numbers typed in contain only numbers or spaces:
do ( if ( !matches(_form.PhoneNumber, "[0-9 ]+"), error("Invalid phone number") ) )
Raising an error in a validation expression - using the error() function - will stop the action at that point. That is, the data in the form will not be saved, and any other tasks in the action (e.g. running task plans) will not go ahead.
- Save your changes to the action
- Drag the action on top of the
Add
button, and theUpdate
button
That completes the configuration. It is useful to see what is going on behind the scenes - click on the Add
button - in the configuration, you can see that by dragging the action on the button, you have set the Action for the button to Validate Contact.
Go into App Mode. Type in a new contact, and include an invalid character in the telephone number - for example, a letter. This should be trapped with an error. You can correct this, then save the new contact.
Similarly, try updating a record, inserting an invalid character to the telephone number. Your validation should prevent you from saving this update.