Customizing the field_error_proc in Rails is causing issues with the HTML layout

Within a rails application, I have customized the field_error_proc to enable inline error displays as shown below:

https://i.sstatic.net/DjmdN.png

The code snippet for achieving this functionality is outlined here:

ActionView::Base.field_error_proc = proc { |html_tag, instance|
  html = %(<div class="field_with_errors">#{html_tag}</div>).html_safe

  form_fields = %w[textarea input select]

  elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css 'label, ' + form_fields.join(', ')

  elements.each do |e|
    next unless form_fields.include?(e.node_name)
    errors = [instance.error_message].flatten.uniq.collect { |error| "#{instance.method_name.humanize} #{error}" }
    html = %(<div class="field_with_errors">#{html_tag}</div><small class="form-text error-text">&nbsp;#{errors.join(', ')}</small>).html_safe
  end

  html
}

This approach works effectively for standard inputs that are wrapped in a usual manner.

An issue arises when the input is enclosed within another element, such as a select2 dropdown or a custom input group illustrated below:

<div class="split-daterange-picker form-control daterange-picker" id="">
  <input class="start-date" placeholder="Requested Dates" type="text" name="housing_lead[start_date]"> 
  <span class="separator"></span>
  <input class="end-date" placeholder="Requested Dates" type="text" name="housing_lead[end_date]"> 
</div>

For my scenario of using two inputs functioning as one form field: https://i.sstatic.net/YHJTb.png

However, when these inputs are enveloped by the field_with_errors div, the display appears as follows:

https://i.sstatic.net/mKdLv.png

To address this concern, my objective is to wrap the split-daterange-picker within the field_with_errors div to allow proper styling and the addition of error messages. How can I achieve this desired outcome?

Answer №1

Unfortunately, there is no straightforward way to achieve this using

ActionView::Base.field_error_proc
. However, you may be able to implement a solution using a custom form builder, as demonstrated in this helpful post.

class CustomFormBuilder < ActionView::Helpers::FormBuilder  
  def create_custom_input(input_field, options = {})
    options[:wrapper_class] ||= []
    if @object.errors[input_field].present?
      options[:wrapper_class] << 'field_with_errors'
    end
    custom_input = text_field(input_field)
    @template.content_tag(:div, custom_input, class: options[:wrapper_class])
  end
end

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The footer seems to be losing its stickiness and not staying at the bottom when I

My goal is to create a sticky footer for my webpage. Once you click the add new sports button, a drawer slides out and the footer remains fixed at the bottom. However, as I scroll down the page, the footer moves up instead of staying in place. I have tried ...

Instructions for integrating a select query within another select query in Codeigniter

I have a question regarding two tables shown below: /* questionnaire table */ | q_id | q_title | | ------- | ---------------------- | | 1 | What is your name? | | ------- | ---------------------- | | 2 | What is your gend ...

What is the procedure for incorporating ajax into Rails 4.0 to perform a basic multiplication function?

Just starting to learn about rails and I have a simple implementation in mind. On the screen, there are two textboxes. In textbox1, I enter a number and upon clicking submit, an ajax request is made. The response should include an HTML template that allows ...

What are the placeholders for the "localIdentName" query tag in a custom CSS-loader for Webpack?

Just starting out with Webpack and experimenting with the css-loader. I came across the option to specify a localIdentName query tag on the Github page under "Local Scope". This tag allows us to define our own custom values for how classes should be named ...

I am looking to have three rows of input text area comments instead of just one

Utilizing Bootstrap 4.3.1 with React Components I currently have this image: https://i.sstatic.net/rudsE.png And I am aiming for this design: https://i.sstatic.net/VmyTW.png comments: { elementLabel: "Comments", elementType: 'textar ...

Remove the color gradient for the column headers in the Google Visualization table

Whenever I attempt to change the colors of the column headers using the method demonstrated in this insightful source, a rather generic gradient is applied. Interestingly, the example code provided also demonstrates the same default gradient on the secon ...

Is the input box failing to show up on your screen?

I recently tackled the task of creating a commenting feature for RSS articles Throughout my journey with PHP coding, I encountered an issue where the input box for comments was not displaying. https://i.stack.imgur.com/gJIlu.png Check out the snippet fr ...

Ways to eliminate the margin space on a Bootstrap 4 progress bar

Currently experimenting with the Bootstrap 4 Progress component and attempting to integrate it within a sentence. Customizing its width to fit between words while realizing it is usually meant to occupy an entire row. Despite reducing its size, there seems ...

Centering an icon in tandem with Typography text using Material UI

Is there a way to align an icon with the text so that they are on the same level? Currently, it seems like the icon is slightly above the text. I've tried using padding-top: 5px and margin-top: 5px, but those solutions didn't work as expected. ...

"Can you provide instructions on how to set the background to the selected button

How can I change the background color of a selected button in this menu? Here is the code for the menu: <ul id="menu"> <li class="current_page_item"><a class="button" id="showdiv1"><span>Homepage</span></a></ ...

Issue with Left Alignment of Tabs in Material-UI

As of now, material-ui's latest version does not provide support for aligning tabs to the left in the component. However, I came across a workaround on this GitHub page I have implemented the same workaround, and you can view it here: Unfortunately, ...

Enhancing WordPress Menu Items with the 'data-hover' Attribute

Looking for a solution to add "data-hover" to menu items on Wordpress, like: Wanting to insert: data-hover="ABOUT US" into <a href="#">ABOUT US</a> without manually editing the link's HTML to make it: <a href="#" data-hover="ABOU ...

Choose not to alter the display value during keyup and keydown events, while still triggering the change event

$(function(){ $(".cls_user_details select").keyup(function(){ $(".cls_user_details select").val("Profile"); }) }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="cls_user_setti ...

Merge floating and absolute positioning techniques

Creating a calendar exhibiting all events in one div requires precise positioning based on the values of top and height. Let me demonstrate this concept. In the image below, both 6am events are aligned vertically. This alignment issue can be resolved by u ...

"Creating a link to a button with the type of 'submit' in HTML - a step-by-step

Found the solution on this interesting page: In a list, there's a button: <form> <ul> <li> <a href="account.html" class="button"> <button type="submit">Submit</button> ...

The issue with using HTML code inside a variable that is passed into a partial view is that the code is not being

I have created a partial view to show the page header, which takes in two variables - an Icon and a title. The code for pageHeader.blade.php is as follows: <div class="page-header"> <div class="row"> <!-- Page header, center on small sc ...

Searching for a way to smoothly scroll horizontally from right to left using the mouse wheel?

I am looking to display 3 out of my collection of over 6 articles, with the ability for the user to scroll from right to left when using the mouse wheel. Additionally, I want to hide the scrollbar for a cleaner look. Can anyone assist me with this? I hav ...

Importing pixi-sound in the right way for PIXI JS

I have a question regarding the proper way to import pixi-sound into my project. I am facing an issue with the following code snippet: import * as PIXI from "pixi.js"; import PIXI_SOUND from "pixi-sound"; const EFFECT_SOUNDS = [...list of music] for (le ...

Flask not serving Favicon and images to a React application

I am currently working with a Flask server and have it set up in the following manner: app = Flask(__name__, static_folder="dist/assets", static_url_path='/assets', template_folder="dist") ...

Is it possible to enable a button as soon as input is entered?

I'm encountering a minor issue with my button's functionality. I am attempting to have the button enabled as soon as text input is entered into the text field, but currently it only becomes enabled after the focus has changed from the text field. ...