turboboost icon indicating copy to clipboard operation
turboboost copied to clipboard

Advanced error handling?

Open Frexuz opened this issue 8 years ago • 3 comments

The current errors when using format.js { render_turboboost_errors_for(@post) } is just a string array of error messages.

I'd like a fine grained error handling, but I don't know which field (attribute) the error corresponds to. Would it be possible to get an object like:

[{ name: 'Name can't be blank' }, { email: 'Email can't be blank' }]

(While retaining the same error output with #error_explanation.)

The reason is that I'd like to style each form field that has an error.

Ideas?

Frexuz avatar Feb 17 '17 08:02 Frexuz

Current workaround:

  1. Don't use Turboboost.insertErrors = true

  2. Overrider the error messages JSON in an initializer.

module Turboboost
  module Controller
    extend ActiveSupport::Concern

    def render_turboboost_errors_for(record)
      render json: record.errors, status: :unprocessable_entity, root: false
    end

  end
end
  1. Use error handling callback
$(document).on('turboboost:error', function(e, errors) {
  //errors is a string in this workaround
  var jsonErrors = JSON.parse(errors),
    $form = $(e.currentTarget);
  //do whatever
});

Would be awesome if your gem would handle this :) I don't have time for a PR at the moment :(

Frexuz avatar Feb 17 '17 11:02 Frexuz

Hi @Frexuz , if you prefer you can use the gem I just launched (turbolinks-form) to replace the whole page body after a submit.

This way you can show your errors anyway you want by re-rendering the form, and you will still have the performance of Turbolinks.

hsgubert avatar Sep 27 '17 17:09 hsgubert

@hsgubert Looks cool :)

Frexuz avatar Sep 28 '17 06:09 Frexuz