Created
December 14, 2020 19:56
-
-
Save herko/8b57c6b15adc36f2033c56bd61f7c1ba to your computer and use it in GitHub Desktop.
Using custom form builder in rails.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/builders/application_form_builder.rb | |
class ApplicationFormBuilder < ActionView::Helpers::FormBuilder | |
def form_group(method, options = {}, &block) | |
content = @template.capture(&block) | |
content += help_block_with_errors(method) if object.errors[method].any? | |
options[:class] ||= "" | |
options[:class] += " form-group" | |
@template.content_tag :div, content, options | |
end | |
def label(method, text = nil, options = {}, &block) | |
text ||= @template.t(".#{method}.label") | |
text += I18n.t("helpers.field.required") if options[:required] | |
options[:class] ||= "" | |
super(method, text, options, &block) | |
end | |
%w[text_field file_field text_area datetime_field email_field password_field url_field].each do |field_helper| | |
define_method field_helper do |method, options = {}| | |
options[:class] ||= "" | |
options[:class] += " form-control" | |
options[:class] += " is-invalid" if object.errors[method].any? | |
options[:placeholder] ||= @template.t(".#{method}.placeholder", default: "") | |
super(method, options) | |
end | |
end | |
def submit(value = nil, options = {}) | |
unless value.is_a? String | |
options = value | |
value = @template.t(".submit") | |
end | |
super(value, options) | |
end | |
private | |
def help_block_with_errors(method) | |
@template.content_tag( | |
:div, object.errors[method].join(", "), class: "invalid-feedback" | |
) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/form_builder.rb | |
# Disable .field_with_errors wrapper for validation messages | |
ActionView::Base.field_error_proc = proc do |html_tag, instance| | |
html_tag.html_safe | |
end | |
# Tell rails which custom form builder to use by default | |
ActionView::Base.default_form_builder = "ApplicationFormBuilder" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%# app/views/registrations/new.html.erb %> | |
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> | |
<%= f.form_group :email do %> | |
<%= f.label :email %> | |
<%= f.text_field :email %> | |
<% end %> | |
<%= f.form_group :nick do %> | |
<%= f.label :nick %> | |
<%= f.text_field :nick %> | |
<% end %> | |
<%= f.form_group :name do %> | |
<%= f.label :name %> | |
<%= f.text_field :name %> | |
<% end %> | |
<%= f.form_group :password do %> | |
<%= f.label :password %> | |
<% if @minimum_password_length %> | |
<small class="form-text text-muted"> | |
<%= t('.min_password_length', length: @minimum_password_length) %> | |
</small> | |
<% end %> | |
<%= f.password_field :password, autocomplete: "off" %> | |
<% end %> | |
<%= f.form_group :password_confirmation do %> | |
<%= f.label :password_confirmation %> | |
<%= f.password_field :password_confirmation, autocomplete: "off" %> | |
<% end %> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sk: | |
user_registrations: | |
form: | |
name: | |
label: Meno a priezvisko | |
placeholder: Ján Galaxián | |
email: | |
label: E-mail | |
placeholder: [email protected] | |
password: | |
label: Heslo | |
password_confirmation: | |
label: Heslo znovu | |
submit: Odoslať |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment