samedi 23 juin 2018

Rails Active Admin base errors - add direct anchor link to the field with error

I have a very long form inside Rails Active Admin.

I would like to customize the base errors messages, that is to say the errors displayed on top of the page (not the ones on each field), so that instead of just being the default one (see example below), the user could have inside this text a "shortlink", that is to say a link to the field with the error so that he can just click and be transported on the page to the field with the error.

Current error message:

enter image description here

Error message i would like to have

For example the text could read:

 "Images at least one image is required. <a href="#link_to_anchor_of_field_with_error">Please check it</a>.

Note that in my Camping form page, I can edit Camping attributes but also the Rooms (they have a belongs to/has_many relation with Camping model=> one camping has many Rooms) So this should be appliable to both attributes of Camping but also to the attributes of the associated rooms if they turn out to have errors on the form submission

ActiveAdmin.register Camping do

  form do |f|
    f.semantic_errors *f.object.errors.keys

    f.inputs "Information for the Camping" do
      f.input :name,        
      f.input :city        
    end

    f.inputs "Rooms" do
      f.has_many :rooms,
                   allow_destroy: true,
                   heading:       false, 
                   new_record:    true,                   
                   sortable_start: 0 do |room|
        room.input :name,          
        room.input :room_nb,         
        room.input :facilities
      end
    end 
  end

I even am not sure if it should be done on config of Active Admin or more inside config of Formtastic, which is the form DSL Active Admin is using. Maybe the customization found in this post (Make semantic_errors render the exact error-message) could be used but it's way beyond my level:)

Aucun commentaire:

Enregistrer un commentaire