WhatsApp / WhatsApp-Flows-Tools

Tools and examples to help you create WhatsApp Flows https://developers.facebook.com/docs/whatsapp/flows
MIT License
45 stars 18 forks source link

how to do server side data validation #12

Closed sanguedemonstro closed 1 month ago

sanguedemonstro commented 1 month ago

It would be great if we had examples of server side data validation, and how to return those validation messages for a given data field, as well as the flow json compatible with that kind of data-exchange response..

For those who are struggling with this scenario, I achieved that by doing the following steps. But be aware that I am not sure if that's the correct way to do that, once I didn't find a clear documentation, so..

data_exchange action response when field_a and field_b are not valid:

{
    "version": "3.0",
    "screen": "SAME_SCREEN",
    "data": {
        "error_messages": {
            "field_a": "Inform only Foo",
            "field_b": "Inform only Bar",
        },
    },
}

Flow JSON, with dynamic error-messages:

"type": "Form",
"name": "form_name",
"init-values": {  },
"error-messages": {
    "field_a": "${data.error_messages.field_a}",
    "field_b": "${data.error_messages.field_b}"
},

If there is another way to do that, please let me know! Thanks

Gaafar commented 1 month ago

Hi @sanguedemonstro! Your approach is correct. The error-messages field is mentioned in the docs here https://developers.facebook.com/docs/whatsapp/flows/reference/flowjson#form-configuration

I think it can be improved by adding some examples. Will raise it with the concerned team.