heartcombo / simple_form

Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
http://blog.plataformatec.com.br/tag/simple_form
MIT License
8.21k stars 1.31k forks source link

Form errors with Rails 7 and Turbo do not work #1789

Closed key88sf closed 2 years ago

key88sf commented 2 years ago

Environment

Current behavior

Using a basic input form in a Rails view for a model:

= simple_form_for(@image) do |f|
  = f.input :url
  = f.button :submit

When there are errors on the model (e.g. create fails due to a validation error on the URL in this case), there are no error classes or visual indication added to the form HTML.

In order to get this to work, Turbo has to be turned off for the form submission: = f.button :submit, data: { turbo: false }

Expected behavior

Would like SimpleForm to "just work" and show validation errors when Turbo is enabled (which it is by default) in Rails 7 apps.

danielricecodes commented 2 years ago

@key88sf - your controller has to respond with code 422 if you want errors to render correctly. Without posting your controller code its impossible to help.

    if @image.save
      redirect_to @image, notice: 'Image successfully created.', status: :see_other
    else
      render :new, status: :unprocessable_entity
    end
danielricecodes commented 2 years ago

This is a duplicate of #1754 and should be closed.

key88sf commented 2 years ago

@danielricecodes Ah yes - the status: is what was missing on the controller side. Thank you for flagging that.