You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/components/form.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -275,6 +275,49 @@ We then get displayed our nice success message (`server says: form submitted suc
275
275
276
276
If we fill in the the input field there and hit the submit button, we not only see the failure messages (`server says: form had errors` and `'foo': [ 'seems to be invalid' ]`), we also get transferred back to the first page, just the way we specified this behavior in the page definition above!
277
277
278
+
### Example 3.1: Async submit request with success transition - dynamically determined by server
279
+
280
+
In the example shown above, the `success``transition` is statically defined. Sometimes the `transition` needs to be dynamically controlled within the server action.
281
+
Imagine creating a new Active Record instance with a `form`. If you want to show the fresh instance on another page and therefore want to define a `transition` after successful form submission, you would need to know the ID of the fresh instance! That is not possible, as the ID is auto-generated and depends on the current environment/state. Therefore you can tell the `form` component to follow a transition, which the server action defines after creating the new instance (and now knowing the ID):
282
+
283
+
On the `page`:
284
+
```ruby
285
+
#...
286
+
287
+
defform_config
288
+
return {
289
+
for::my_object,
290
+
method::post,
291
+
path::success_form_test_path,
292
+
success: {
293
+
emit:'my_form_success',
294
+
transition: {
295
+
follow_response:true# follow the serverside transition
296
+
}
297
+
}
298
+
}
299
+
end
300
+
```
301
+
On the `controller``action`:
302
+
303
+
```ruby
304
+
#...
305
+
defmodel_submit
306
+
@test_model=TestModel.create(model_params)
307
+
if@test_model.errors.any?
308
+
render json: {
309
+
message:'server says: something went wrong!',
310
+
errors:@test_model.errors
311
+
}, status::unproccessable_entity
312
+
else
313
+
render json: {
314
+
message:'server says: form submitted successfully!',
315
+
transition_to: some_other_path(id:@test_model.id) #tell the form component where to transition to with the id, which was not available before
316
+
}, status::ok
317
+
end
318
+
end
319
+
```
320
+
278
321
### Example 4: Multiple input fields of different types
279
322
280
323
Of course, our input core component accepts not only 'text', but very different input types: In this example, we will introduce 'password', 'number', 'email', 'textarea' types!
0 commit comments