From db179c4ef5eb7833ee518ef302580f6a1998bc13 Mon Sep 17 00:00:00 2001 From: Judith Roth Date: Mon, 24 Jun 2024 16:04:38 +0200 Subject: [PATCH] Document how to render validation errors inside a dialog with a form --- lookbook/docs/patterns/05-dialogs.md.erb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lookbook/docs/patterns/05-dialogs.md.erb b/lookbook/docs/patterns/05-dialogs.md.erb index 5444a136bae5..1da60e6ab5e5 100644 --- a/lookbook/docs/patterns/05-dialogs.md.erb +++ b/lookbook/docs/patterns/05-dialogs.md.erb @@ -127,6 +127,29 @@ end %> ``` +### Dialogs with form validations + +Sometimes a dialog with a form needs validations. If the form is submitted with turbo, the form can be rendered again with the validation errors within the dialog. +In order for the dialog to not be closed, the rendering has to happen with an error response code (therefore 4xx or 5xx). + +```ruby +class TestController < ApplicationControler + # include the helper module + include OpTurbo::DialogStreamHelper + + # ... + + def update + if @my_model.save + # ... + else + component = ComponentThatRendersAForm.new(my_model: @my_model) + update_via_turbo_stream(component: component, status: :bad_request) + end + respond_with_turbo_streams + end +end +``` ## Special kinds of dialogs