Tristan O'Neil
Tristan O'Neil

Reputation: 331

Datamapper validation errors with Sinatra

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.

Upvotes: 1

Views: 928

Answers (1)

Kazi Manzur Rashid
Kazi Manzur Rashid

Reputation: 1544

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

Upvotes: 2

Related Questions