Not sure if bug, or intended behaviour… Need some advice:
I have a form that is sending 2 arrays of information to a server action.
The server action loops through the values with a ‘repeat’.
Everything works as expected, however when I add a ‘Response’ it will only perform that repeat action one time.
When I disable it it’ll repeat the correct amount of times.
It seems like the ‘response’ is telling the server action that it’s done?
Is there a way around it? I want to display on the front-end that the form submit has failed or succeeded.
That is responding an error or a success message back to the client.
How can I do that in this case?
Example:
It’s trying to add 2 records, and I want to show 2 toasts:
The first one fails (I want to display a toast error on page - by sending the response to the page)
Second one succeeds (I want to display a toast ‘success’ on page)
Edit:
Only thing I can think of right now is to create an array in the server action, and then output that at the end of the server action OUTSIDE of the repeat?
So you are using responses just to show a toast on the page?
If that’s the case … just use a setvalue for the else step instead and check its value on the front end.
Last question:
It’s only showing me one toast though.
I have to put the toast in a repeat for every value or is there an easier way? (Right now you can see that it’s taking repeat[0] and repeat_for_courses[0])
I am not completely sure i understand the structure of your server action and what are you doing exactly and how many toast need to be displayed / where / why, so i can’t really answer your last question.
Users_courses - One record in this table links a user to a course. If they’re linked together that means that the user can access that course
Goal: I need a form where I multi-select users and courses, on submit it’ll add records to users_courses for every course+user combination that doesn’t already exist.
The server action:
Repeat over every user that’s sent via the form
For each user, repeats over the courses that are sent via the form
If the course+user combination exists: setvalue "course is already assigned"
If the course+user combination doesn’t exist: `database insert into users_courses AND setvalue “course succesfully assigned”
This is working perfectly, I just want front-end information
Now example:
I select 5 courses, 3 users.
1 of those users already has those 5 courses assigned
After form submit I want to see something on the page like: “10 courses assigned, 5 failed”