Skip to content

Commit

Permalink
Move validation to field_error_proc
Browse files Browse the repository at this point in the history
  • Loading branch information
lazaronixon committed Aug 16, 2022
1 parent 5d3a1d8 commit 07abe82
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
11 changes: 11 additions & 0 deletions lib/generators/admin/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ class InstallGenerator < Rails::Generators::Base

source_root File.expand_path("templates", __dir__)

def add_field_error_proc
field_error_proc_code = <<~RUBY
# Provides an HTML generator for displaying errors that come from Active Model
config.action_view.field_error_proc = Proc.new do |html_tag, instance|
raw Nokogiri::HTML.fragment(html_tag).child.add_class("is-invalid")
end
RUBY

environment field_error_proc_code
end

def add_gems
uncomment_lines "Gemfile", /"bcrypt"/
gem "pagy", comment: "Use Pagy to add paginated results [https://github.com/ddnexus/pagy]"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% message = notice || alert %>

<% if message %>
<% if message.present? %>
<div class="position-fixed start-50 translate-middle-x" style="z-index: 1030; top: 8rem;">
<div class="toast hide text-center text-white bg-dark border-0" data-controller="flash-message">
<div class="toast-body">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@
import { Application, Controller } from "https://cdn.jsdelivr.net/npm/@hotwired/[email protected]/dist/stimulus.min.js"
const stimulus = Application.start()

stimulus.register("validation", class extends Controller {
connect() {
this.element.querySelectorAll(".field_with_errors").forEach((element) => {
element.firstChild.classList.add("is-invalid")
element.parentNode.insertBefore(element.firstChild, element)
element.parentNode.removeChild(element)
})
}
})

stimulus.register("flash-message", class extends Controller {
connect() {
new bootstrap.Toast(this.element).show()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_with(model: user, html: { "data-controller": "validation" }) do |form| %>
<%= form_with(model: user) do |form| %>
<div class="form-group row mb-2">
<%= form.label :email, class: "form-label col-md-3 col-form-label" %>
<div class="col-md px-0">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%%= form_with(model: <%= model_resource_name %>, html: { "data-controller": "validation" }) do |form| %>
<%%= form_with(model: <%= model_resource_name %>) do |form| %>
<% attributes.each do |attribute| -%>
<div class="form-group row mb-2">
<% if attribute.password_digest? -%>
Expand Down

0 comments on commit 07abe82

Please sign in to comment.