Module: ConstraintValidations::FormBuilder::Extensions

Included in:
ConstraintValidations::FormBuilder
Defined in:
lib/constraint_validations/form_builder/extensions.rb

Instance Method Summary collapse

Instance Method Details

#errors(field, &block) ⇒ Object

Delegates to the FormBuilder#object property when possible, and returns any error messages for the field argument. When passed a block, #errors will yield the error messages as the block’s first parameter

Examples

<span><%= form.errors(:subject).to_sentence %></span>

<% form.errors(:subject) do |messages| %>
 <h2><%= pluralize(messages.count, "errors") %></h2>

 <ul>
   <% messages.each do |message| %>
     <li><%= message %></li>
   <% end %>
 </ul>
<% end %>


116
117
118
# File 'lib/constraint_validations/form_builder/extensions.rb', line 116

def errors(field, &block)
  FormBuilder.errors(object, field, &block)
end

#initializeObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/constraint_validations/form_builder/extensions.rb', line 4

def initialize(*)
  super

  @validation_message_template =
    if (parent_builder = @options[:parent_builder]) &&
        (inherited_validation_message_template = parent_builder.instance_values["validation_message_template"])
      inherited_validation_message_template
    else
      proc { |messages, tag| tag.span(messages.to_sentence) }
    end
end

#validation_message(field, message: nil, **attributes, &block) ⇒ Object

When the form’s model is invalid, validation_message renders HTML that’s generated by iterating over a field’s errors and passing them as paramters to the block captured by the form’s call to validation_message_template. The resulting element’s id attribute will be generated by validation_message_id to be referenced by field elements’ aria-describedby attributes.

One-off overrides to the form’s validation_message_template can be made by passing a block to validation_message.

Examples

<%= form.validation_message :subject %>
<%# => <span id="subject_validation_message">can't be blank</span> %>

<% form.validation_message :subject do |errors, tag| %>
  <%= tag.span errors.to_sentence, class: "special-error" %>
<% end %>
<%# => <span id="subject_validation_message" class="special-error">can't be blank</span> %>


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/constraint_validations/form_builder/extensions.rb', line 73

def validation_message(field, message: nil, **attributes, &block)
  errors field do |messages|
    if message.present?
      messages = Array(message)
    end

    @template.tag.with_options id: validation_message_id(field), **attributes do |tag|
      block ? yield(messages, tag) : @validation_message_template.call(messages, tag)
    end
  end
end

#validation_message_id(field, index: @index, namespace: ) ⇒ Object

When the form’s model is invalid, validation_message_id generates and returns a DOM id attribute for the field, otherwise returns nil

Examples

<%= form.text_field :subject, aria: {describedby: form.validation_message_id(:subject)} %>


93
94
95
# File 'lib/constraint_validations/form_builder/extensions.rb', line 93

def validation_message_id(field, index: @index, namespace: @options[:namespace])
  FormBuilder.validation_message_id(@template, @object_name, field, index: index, namespace: namespace)
end

#validation_message_template(&block) ⇒ Object

Captures a block for rendering both server- and client-side validation messages

The block accepts two arguments: errors and tag. The errors argument is an Array of message Strings generated by an ActiveModel::Errors instance. The tag argument is an ActionView::Helpers::TagHelpers::TagBuilder instance prepared to render with an id attribute generated by a call to validation_message_id.

The resulting block will be evaluated by subsequent calls to validation_message and will serve as a template for client-side Constraint Validation message rendering.

Examples

<%= form.validation_message_template do |messages, tag| %>
  <%= tag.span messages.to_sentence, style: "color: red;" %>
<% end %>
<%# => <template data-validation-message-template> %>
<%#      <span style="color: red;"></span>         %>
<%#    </template>                                 %>

<%= form.validation_message :subject %>
<%# => <span style="color: red;">can't be blank</span> %>


43
44
45
46
47
48
49
# File 'lib/constraint_validations/form_builder/extensions.rb', line 43

def validation_message_template(&block)
  @validation_message_template = block unless block.nil?

  content = @template.capture { @validation_message_template.call([], @template.tag) }

  @template.tag.template content, data: {validation_message_template: true}
end