How do I connect one form to different server actions?

Hello All!

Here is the page on which the question arose: http://co43498.tmweb.ru/page2.html

Before creating a question, I tried to find the answer in routing-related topics. But the fact that I managed to find seem to consider a few other things.

I’m trying to make it possible to do different actions with table entries by selecting them from the list of possible actions.

Now I have two server actions configured:

  1. Deletes the selected records.
  2. Update of selected records.

The problem, however, is that the form in which the table resides can only connect to one server action. Hence the question:

  • How to make the link to the server action in the form change depending on the selected action from the list?

if I understood clearly you can use server side condition

so you have to sent id for actions

if selectedAction == 2 use update action
if selectedAction == 3 use delete action

1 Like

Hello @Mr.Rubi

What do you mean to use the same form? I see you open a modal for update record - why not assign the update record there? Or do i not understand maybe what exactly are you trying to do?

1 Like

All my table is in form. This is done to ensure that the action server has received the correct data on the selected checkboxes of the records.

Both modal Windows are inside the same form and work simply as a subscription button.

In order for the form to work and send data, we must prescribe link the action of the server to which this form is connected. My question:

  • How to dynamically change the link to the server action, which we prescribe in the form properties?

PS At the moment my form is connected only to the action of the server responsible for the update records. Therefore, no matter what you choose in the interface will always be an update. The plan should be different, when you select delete in the interface, the form should be connected to the action of the server responsible for deleting and accordingly delete the selected records.

Well there are two ways of doing this.

  1. Use separate actions and separate forms in the modals.
    or
  2. Use the condition step in your server action (to check some hidden field or some value) and add the Update step in “then” and Delete Step in “else”.

I would just prefer doing this with separate server actions and separate forms, but that is my opinion.

1 Like

The second way I seem to understand is to send everything to a very complex server action that does the action based on the conditions. But if I lay a large number of possible actions, it will be very cumbersome.

As for the first option: I do not understand how I can send data on checkboxes from one form to different server actions?

Before you can delete or update records, you must select them. The selection is made by checkboxes and sent to the server using the form. In properties of this form I can register only one action of the server. In other words, the data on the checkboxes can be used only for the delete action or for the update action.

How do I make the form that is responsible for sending data to checkboxes send data to the selected server action?

You can add a hidden field in the delete form. Then assign to it the array value (the values of selected checkboxes) and send it to the delete action in your modal form.

1 Like

I think I understand :smiley:

I will try to implement

1 Like

And what field is better suited for storing data by checkboxes?

@Teodor needs a hint. I understood the logic that each server action will have its own form. And I realized that in the form it is necessary to set an invisible field in which the values of the checkboxes from the table will be taken as a dynamic attribute. However, I can not organize this transfer. I do not fully understand what field should be used for this? I use “array” and fill in the “Items” row in the properties, selecting the checkbox values in the table as the data source. But I feel like I’m doing something wrong. Could you direct me?

Thank you in advance.

Just an idea but send the form to a single server action with an additional parameter for each separate action (or use an existing one as appropriate) then use a conditional statement in the server action to call the two different scripts using core actions=> redirect. may have to set some sessions to pass parameters though.

Thank you @Hyperbytes for idea.

The option of one complex server-side action, I also realized. But in this case it turns out to be cumbersome and quite confusing (because there can be many different scenarios).

I really liked the idea of @Teodor to remove the form from the main table and make for each server action (scenario) its own form, where to send an array of data by checkboxes. In this way, you can make as many scenarios as you want to work with the records of the table and all of it will be carried out by a separate form and a separate server action. This structure seems to me to be more optimized and understandable.

The only question is how to send an array of data on the checkboxes from the table to the form field (there will be many forms for each server action) in order to use this data in the server action in the future?

Hello @Teodor

Please help in explaining what exactly need to add to the form for each process to be able to send data for checkboxes? Unfortunately I can not be solved this problem yourself.

Thank you in advance for your help.

Hello,
The logic is to store the checkbox values in the array component.
Then use your array component.items as a value for the hidden field in your modal.

Looking at what you have on the page, this should be added to your checkboxes:

dmx-on:change="id_checkbox_cell.checked ? yourArray.remove(value) : yourArray.addUniq(value)"

Then in your form, select the hidden field, dynamic attributes > input > value and add the yourArray.items as a value. This will pass the checked checkboxes values to the server action.

NOTE: replace yourArray with your array component name.

1 Like

I do everything according to the instructions, but apparently somewhere I make a mistake and can not find it.

That’s what I do:

  1. I created an array on the page:
    <dmx-array id="id_array_update_checkbox"></dmx-array>

  2. I added additional settings to the checkboxes in the table, now their code looks like this:
    <input class="form-check-input" type="checkbox" value=" id="id_checkbox_cell" name="checkbox_cell[]" dmx-bind:value="id_user" dmx-bind:checked="id_checkbox_all.checked" dmx-on:updated="id_checkbox_cell.checked == true ? arrayChecked.addUniq($index) : arrayChecked.remove($index)" dmx-on:changed="id_checkbox_cell.checked ? id_array_update_checkbox.remove(value) : id_array_update_checkbox.addUniq(value)">

  3. Then I added a hidden field to the form and added a dynamic value attribute from the array:
    <input id="id_arrayCheckbox_cell" name="arrayCheckbox_cell" type="hidden" class="form-control" dmx-bind:value="id_array_update_checkbox.items">

  4. In the server I am connecting the form, and fields. Field from array with checkboxes I mark as Multiple (1):

  5. The condition in the request I leave the same as before (2):

I would be grateful if you could tell me where I made a mistake.

Sorry it must be dmx-on:change (without the d)

dmx-on:change="id_checkbox_cell.checked ? id_array_update_checkbox.remove(value) : id_array_update_checkbox.addUniq(value)"```
1 Like

The code has fixed, but still does not work.

What is this value dmx-bind:items="tableRepeat2[0].id_checkbox_cell.value" you are using here in the array?

<dmx-array id="id_array_update_checkbox" dmx-bind:items="tableRepeat2[0].id_checkbox_cell.value"></dmx-array>

This value is gone, I deleted it. It’s old code.

Ok, now in the server action, remove the repeat and try using the update step alone, for the filter use “IN” and the array value.