Module: Padrino::FormErrors::Helpers::FormHelpers

Defined in:
lib/padrino-form-errors/helpers/form_helpers.rb

Instance Method Summary collapse

Instance Method Details

#error_messages_for(*objects) ⇒ Object

It displays validation errors for given object. Here is an HAML example:

Examples

- form_tag url(:users) do
  = error_messages_for(@user)
  -# ...


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/padrino-form-errors/helpers/form_helpers.rb', line 14

def error_messages_for(*objects)
  errors = 0
  objects.map! do |object| 
    if object.is_a? Symbol 
      instance_variable_get("@#{object}")
    elsif object.respond_to? :object
      object = object.object
    else
      object 
    end
    errors += object.errors.size and object
  end
  if errors > 0
    partial "shared/error_messages", :locals => { :object => objects.first, :objects => objects }
  end 
end

#error_messages_on(object, field) ⇒ Object

It displays inline error for single field.

Examples

- form_tag url(:sessions_login) do
  = label "Login", :for => :login
  = text_field_tag :login
  = error_messages_on(@session, :login)
  -# ...


42
43
44
45
46
47
48
# File 'lib/padrino-form-errors/helpers/form_helpers.rb', line 42

def error_messages_on(object, field)
  object = instance_variable_get("@#{object}") if object.is_a?(Symbol)
  error = object.errors[field] rescue nil
  if error
    partial "shared/error_message", :locals => { :object => object, :field => field, :error => error }
  end
end