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 stepsstep, 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:
...
- 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
Note |
---|
Copy values Values by name 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:
Code Block |
---|
do (
if ( !matches(_form.PhoneNumber, "[0-9 ]+"),
error("Invalid phone number")
)
) |
Note |
---|
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.
Validate business contacts have business name
Next we'll add some additional validation to check that business contacts have a business name set:
Widget Connector | ||||||
---|---|---|---|---|---|---|
|
Add this validation to your app. To recap the steps in the video:
- Open details for the action
Validate Contact
- you can either open the stream configuration form forContacts
, and find the list of actions; or click on theAdd
orUpdate
button, and from the button configuration form, follow the quick link to the action details - Add a condition to the validation expression to check that for business contacts, the business name is set; make sure you remember you put a comma after the
if()
statement you already have in your expression, then add:
Code Block |
---|
if ( (_form.ContactType == "Business") && (isEmpty(_form.BusinessName)),
error("Business name must be set for business contacts")
) |
- Save changes to your action
Go into App Mode. Check that the error is shown when users forget to enter a business name for a business contact.
Panel |
---|
Next chapter: 8. Setting values in actions |