Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pablobm/fields_enclosure

A Ruby on Rails plugin. Create "enclosures" inside forms that will receive the class name "containsErrors" when one or more enclosed fields have errors.
https://github.com/pablobm/fields_enclosure

Last synced: 15 days ago
JSON representation

A Ruby on Rails plugin. Create "enclosures" inside forms that will receive the class name "containsErrors" when one or more enclosed fields have errors.

Awesome Lists containing this project

README

        

h1. fields_enclosure

h2. Introduction

_fields_enclosure_ is a plugin for Ruby on Rails.

In short. Take the following ERB code that makes use of FormBuilder:


<% form_for @thing do |f| %>
<%= f.enclosure %><%= f.text_field :title %><% end %>
<%= f.enclosure %><%= f.text_area :body %><% end %>
<% end %>

Assuming that the field *_title_ has an error* (in the ActiveRecord sense), the following output is produced:





Body of the thing


h2. A bit more of detail

This plugin allows you to define HTML tags (_enclosures_) that will be given a class name _containsErrors_ when any of the fields contained within has an ActiveRecord error. This requires the use of FormBuilder.

The _enclosure_ method accepts two arguments, both optional:



# enclosure(enclosing_tag = 'div', options = {})
#
# - enclosing_tag: tag name of the HTML element that will
# surround the enclosed block. By default,
# it will be a DIV.
# - options: will be passed on to the _content_tag()_ call what will
# ultimately generate the enclosing tag. Typically it
# will contain HTML attributes.

h2. Further tweaking

h3. Generated output

If you don't like the HTML output produced by the _enclosure_ call, you may easily adapt it to your own needs. The default implementation provided by the plugin is the following:


@@enclosure_proc = Proc.new do |template, enclosing_html_tag, enclosed_html, has_errors, options|
options[:class] = options[:class] ? (options[:class] + ' containsErrors') : 'containsErrors' if has_errors
template.content_tag(enclosing_html_tag, enclosed_html, options)
end
cattr_accessor :enclosure_proc

In order to tweak it, simply redefine _enclosure_proc_ in your confing/environment.rb (or even better, in one of your initializers):



ActionView::Base.enclosure_proc = Proc.new do |template, enclosing_html_tag, enclosed_html, has_errors, options|
# Do as you wish here. Received parameters are:
# - template: an instance of ActionView::Base you'll need if you plan to use helper methods
# - enclosing_html_tag: first argument passed on the f.enclosure() call
# - enclosed_html: the HTML output of the enclosed block
# - has_errors: does the enclosed block contain any ActiveRecord errors?
# - options: second (and optional) argument passed on the f.enclosure() call
end

h3. What about the fieldWithErrors bit?

On a related note, you may also want to change the way Rails renders those tags with the class name _fieldWithErrors_. That's allowed by rails without the need of any plugin. See the example:


ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" }

For details, check out "this post":http://blog.imperialdune.com/2007/3/25/fieldwitherrors .

Copyright (c) 2008 Pablo Brasero Moreno, released under the MIT license