Posting Complex Data Objects to Server Side

Intro

Often you need to send complex data structures with form submissions, for example sending your shopping cart data to the server side. The Form Data component makes it easy to send such data to your server actions.

Page Setup

In our example we will show you how to send the contents of the Data Store component to a server action.
We have a really simple shopping cart page, where you can add products to a data store and you can submit an order via a Server Connect Form. We want to send the contents of the Data Store to the server action using this same form:

The Data Store has a few fields:

Right click the form:

And add the Form Data component:

Select the Form Data component and click the dynamic data picker button in order to select what data to be sent to the server action:

Select data under the Data Store component

And you are done:

On form submit the Form Data component will send the Data Store data.

Server Action Setup

Open the Server Action used with your form and select Input:

Then browse to your page, select the form from the dropdown and click the Import button:

Your form inputs as well as the Form Data component will be imported under $_POST. The schema of the Form Data component will be imported as well, so that you can work with it in your server action steps:

For test purposes we will output the $_POST variables values in set value steps. We output the value of the Form Data component as well:

When we add some products to the shopping cart (data store) and submit the form, in the browser dev tools we can see the Set Value step called products, outputs the data store component data:

That’s how to post complex data objects with your form submissions.

12 Likes

This is an excellent addition to the toolbox :slight_smile:

1 Like

This is actually a very useful update. In some cases, this will help you create much simpler, faster, and better complex solutions. Thanks! :beers:

However, even in the example, the Wappler’s Achilles heel is visible - working with data types. You send some data to the server and on the way back they can easily be converted into something else (mostly of course everything turns into text). It can be seen from the example that in fact 4 out of 5 fields in the array should be numbers, but in the response from the server everything came in the form of text.

There is a great lack of type control in the Wappler. So that when sending data, you can specify which type the field has. The server modules had the same control, both at the input and at the output. This would allow you to always control the type of input and output data and avoid a lot of errors in complex projects related to the data type.

This is not a limitation of Wappler it is the whole purpose when submitting forms according to the html standards and their form data - it is all submitted as text according to the standards.

What you probably need is an json data submit which is a different use case than the one explained above.

Nice addition guys!

I’m dealing with this kind of functionality now… Couldn’t that be a week earlier? :stuck_out_tongue_winking_eye:

Anyway, would need some help/ideas on this one.
Since we are talking about a datastore (and we know it needs a bit of caution on this) how would you handle validation on the above (your axample) case?
On server-side we can validate the POST variables but can we assign/connect the server-side validation of a field on the client side?
Haven’t used it yet so I’m talking too soon… This is just the first thing that came in mind!