extraction d'un composant pour les erreurs de formulaires

This commit is contained in:
Jérémy Lecour 2021-03-04 22:55:09 +01:00 committed by Jérémy Lecour
parent f5a5b50831
commit 415e240406
8 changed files with 56 additions and 36 deletions

View File

@ -1,4 +1,4 @@
<div class="rounded-md bg-green-50 p-4">
<div class="rounded-md bg-green-50 p-4 mb-4">
<div class="flex">
<div class="flex-shrink-0">
<!-- Heroicon name: solid/information-circle -->

View File

@ -0,0 +1,24 @@
<!-- This example requires Tailwind CSS v2.0+ -->
<div class="rounded-md bg-red-50 p-4 my-4">
<div class="flex">
<div class="flex-shrink-0">
<!-- Heroicon name: solid/x-circle -->
<svg class="h-5 w-5 text-red-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3">
<h2 class="text-sm font-medium text-red-800">
<%= content %>
</h2>
<div class="mt-2 text-sm text-red-700">
<ul class="list-disc pl-5 space-y-1">
<% @errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
</div>
</div>
</div>

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
class FormErrorsComponent < ViewComponent::Base
def initialize(errors:)
@errors = errors
end
end

View File

@ -1,16 +1,8 @@
<%= form_with(model: [ condition.filter, condition ],
data: { controller: "reset_form", action: "turbo:submit-end->reset_form#reset" }) do |form| %>
<% if condition.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(condition.errors.count, "error") %> prohibited this condition from being saved:</h2>
<ul>
<% condition.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= render(FormErrorsComponent.new(errors: condition.errors)) do %>
<%= pluralize(condition.errors.count, "error") %> prohibited this condition from being saved:
<% end if condition.errors.any? %>
<div class="field flex items-center">
<fieldset>

View File

@ -1,16 +1,8 @@
<%= turbo_frame_tag "filter" do %>
<%= form_with(model: filter) do |form| %>
<% if filter.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(filter.errors.count, "error") %> prohibited this filter from being saved:</h2>
<ul>
<% filter.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= render(FormErrorsComponent.new(errors: filter.errors)) do %>
<%= pluralize(filter.errors.count, "error") %> prohibited this filter from being saved:
<% end if filter.errors.any? %>
<div class="field">
<%= form.label :description, class: "block text-sm font-medium text-gray-700" %>

View File

@ -4,8 +4,6 @@
<%= render(FlashNoticeComponent.new) { notice } if notice.present? %>
<%= link_to '← Back to filters list', filters_path %>
<%= turbo_frame_tag "filter" do %>
<%= render @filter %>
<% end %>
@ -30,3 +28,7 @@
<% end %>
</div>
</div>
<div class="my-4">
<%= link_to '← Back to filters list', filters_path, class: "hover:text-blue-700" %>
</div>

View File

@ -1,16 +1,8 @@
<%= form_with(model: [ operation.filter, operation ],
data: { controller: "reset_form", operation: "turbo:submit-end->reset_form#reset" }) do |form| %>
<% if operation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(operation.errors.count, "error") %> prohibited this operation from being saved:</h2>
<ul>
<% operation.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= render(FormErrorsComponent.new(errors: operation.errors)) do %>
<%= pluralize(operation.errors.count, "error") %> prohibited this operation from being saved:
<% end if operation.errors.any? %>
<div class="field flex items-center">
<fieldset>

View File

@ -0,0 +1,10 @@
require "test_helper"
class FormErrorsComponentTest < ViewComponent::TestCase
def test_component_renders_something_useful
# assert_equal(
# %(<span>Hello, components!</span>),
# render_inline(FormErrorsComponent.new(message: "Hello, components!")).css("span").to_html
# )
end
end