Skip to content

Commit

Permalink
Added front-end validation to the contact form. Backend would be nice…
Browse files Browse the repository at this point in the history
…, as would Mollom validation.
  • Loading branch information
chadoh committed May 12, 2010
1 parent 8bf9367 commit 0bb52dd
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 22 deletions.
25 changes: 11 additions & 14 deletions controls.rb
Expand Up @@ -86,6 +86,7 @@ def method_missing(name)
feed = Crack::XML.parse(xml).feed feed = Crack::XML.parse(xml).feed
@posts = feed.entry @posts = feed.entry


#make work when offline, give an error


haml :thoughts haml :thoughts
end end
Expand All @@ -103,24 +104,20 @@ def method_missing(name)
end end


post "/contact" do post "/contact" do
smtp_options = { :address => 'smtp.sendgrid.net', #form validation in Sinatra?
:port => '25', #put it here!
:authentication => :plain, Pony.mail :to => 'chad.ostrowski@gmail.com',
:user => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN']
}
mail_options = { :to => 'chad.ostrowski@gmail.com',
:from => '"' + params[:name] + '" <' + params[:email] + '>', :from => '"' + params[:name] + '" <' + params[:email] + '>',
:body => params[:message], :body => params[:message],
:subject => "Mail from Lisli.net!", :subject => "Mail from Lisli.net!",
:via => :smtp, :via => :smtp,
:via_options => smtp_options :via_options => { :address => 'smtp.sendgrid.net',
} :port => '25',
puts mail_options.keys :authentication => :plain,
puts smtp_options.keys :user => ENV['SENDGRID_USERNAME'],
Pony.mail mail_options :password => ENV['SENDGRID_PASSWORD'],

:domain => ENV['SENDGRID_DOMAIN']
}
flash.now[:notice] = "Thanks for your message! I'll get back to you soon." flash.now[:notice] = "Thanks for your message! I'll get back to you soon."
haml :contact haml :contact
end end
154 changes: 154 additions & 0 deletions public/javascripts/jquery-1.4.2.min.js

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions public/javascripts/jquery.validate.min.js

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions public/javascripts/validation.js
@@ -0,0 +1,20 @@
$(function() {

$("form").validate({
debug: true,
messages: {
email: {
required: "I need your email address, please!",
email: "I need your <em>full</em> email address, like name@example.com"
},
message: "I know you can muster something more interesting!"
},
highlight: function(element, errorClass, validClass) {
$(element).addClass(errorClass).removeClass(validClass);
},
unhighlight: function(element, errorClass, validClass) {
$(element).removeClass(errorClass).addClass(validClass);
}
});

});
14 changes: 12 additions & 2 deletions views/contact.haml
@@ -1,9 +1,19 @@
- content_for :validation do
%script(type="text/javascript" src="javascripts/jquery-1.4.2.min.js")
%script(type="text/javascript" src="javascripts/jquery.validate.min.js")
%script(type="text/javascript" src="javascripts/validation.js")
%h2 Contact Me %h2 Contact Me
%form{ :name => "contact_me", :id => "contact_me", :action => "/contact", :method => "post" } %form{ :name => "contact_me", :id => "contact_me", :action => "/contact", :method => "post" }
%label{ :for => "name" } Your name %label{ :for => "name" } Your name
%input{ :name => "name", :id => "name", :type => "text" } %input{ :name => "name", :id => "name", :type => "text" }
%br
%br
%label{ :for => "email" } Your email address %label{ :for => "email" } Your email address
%input{ :name => "email", :id => "email", :type => "text" } %input.required.email{ :name => "email", :id => "email", :type => "text" }
%br
%br
%label{ :for => "message" } Your message to me %label{ :for => "message" } Your message to me
%textarea{ :name => "message", :id => "message" } %textarea.required{ :name => "message", :id => "message", :minlength => "10" }
%br
%br
%input{ :type => "submit", :value => "Send" } %input{ :type => "submit", :value => "Send" }
6 changes: 3 additions & 3 deletions views/layout.haml
Expand Up @@ -5,11 +5,11 @@
%title Lisli %title Lisli
%link(rel="stylesheet" type="text/css" href="/styles.css") %link(rel="stylesheet" type="text/css" href="/styles.css")
%link(rel="shortcut icon" href="images/favicon.ico") %link(rel="shortcut icon" href="images/favicon.ico")
=yield_content :validation
%body %body
#header #header
%h1#blog-title Lisli %h1#blog-title Lisli
%p#description %p#description

#nav #nav
%ul %ul
%li %li
Expand Down Expand Up @@ -38,5 +38,5 @@
=haml :previous_posts, :layout => false =haml :previous_posts, :layout => false
=haml :archives, :layout => false =haml :archives, :layout => false
=yield_content :subscribe =yield_content :subscribe
#footer #footer
%p %p
21 changes: 18 additions & 3 deletions views/styles.sass
Expand Up @@ -87,6 +87,9 @@ a
div div
display: block display: block


div.feedback
display: none

#blog-title #blog-title
border: solid #eee border: solid #eee
border-width: 1px 1px 0px border-width: 1px 1px 0px
Expand Down Expand Up @@ -306,10 +309,21 @@ input[type="text"], textarea
padding: 7px padding: 7px
display: block display: block
font: large Georgia, serif font: large Georgia, serif
margin: 2px 0 37px margin: 2px 0 10px
textarea textarea
height: 150px height: 150px


input.error, textarea.error
background: #fed

label.error[generated=true]
background: #fff
color: #c60
padding: 0
-webkit-box-shadow: none
-moz-box-shado: none
box-shadow: none

input[type="submit"] input[type="submit"]
background-color: #666 background-color: #666
padding: 6px 12px padding: 6px 12px
Expand All @@ -332,13 +346,14 @@ input[type="submit"]
input[type="text"], textarea, #notice, label input[type="text"], textarea, #notice, label
-webkit-box-shadow: 0 0 3px #999 -webkit-box-shadow: 0 0 3px #999
-moz-box-shadow: 0 0 3px #999 -moz-box-shadow: 0 0 3px #999
box-shadoh: 0 0 3px #999


#notice #notice
background: #ffe099 background: #fed
border: 2px solid #c60 border: 2px solid #c60
margin: 0 0 20px margin: 0 0 20px
padding: 10px padding: 10px
text-align: center text-align: center


::selection ::selection
background: #ffe099 background: #fed

0 comments on commit 0bb52dd

Please sign in to comment.