views:

23

answers:

1

I'm trying to validate my Datamapper models and show the validation errors in my HAML template, I'm using Sinatra. I'm at loss how to save those errors into an instance variable and access them from my template. I've looked around for some documentation or tutorials explaining how to do something like this but I haven't been able to find anything.

+1  A: 

Here you go

haml view:

    %label{:for => "title"} Title
    %input#title{:name => "title", :type => "text", :size => "22", :maxlength => "256", :required => true, :placeholder => "Title...", :autofocus => true, :value => @post.title}/
    = field_validation(@post, :title)

helper method:

def field_validation(target, field)
 "<span class=\"field-validation-error\">#{target.errors[field][0]}</span>" unless target.errors[field].empty?
end
kazimanzurrashid