Module: Sinatra::FormKeeper
- Defined in:
- lib/sinatra/formkeeper.rb,
lib/sinatra/formkeeper/version.rb
Overview
Sinatra::FormKeeper
Example:
require 'sinatra/base'
require 'sinatra/formkeeper'
module MyApp < Sinatra::Base
register Sinatra::FormKeeper
def post '/login' do
form do
filters :strip
field :username, :present => true, :length => 4..8, :ascii => true
field :password, :present => true, :length => 4..8, :ascii => true
end
# check if validation has failed or not
if form.failed?
output = erb :login
fill_in_form(output)
else
# you can use filtered, valid parameters through form[key]
authenticate(form[:username], form[:password])
...
redirect '/mypage'
end
end
end
@@ login
<html>
<head><title>Login</title></head>
<body>
<% if form.failed? %>
<p>Found invalid input, check it and try again.</p>
<% end %>
<form action="/login" method="post">
<label>name</label><input type="text" name="username" /><br />
<label>password</label><input type="password" name="password" /><br />
<input type="submit" value="login">
</form>
</body>
</html>
Defined Under Namespace
Modules: Helpers
Constant Summary collapse
- VERSION =
"0.0.7"
Class Method Summary collapse
Instance Method Summary collapse
- #form_constraint(name, &block) ⇒ Object
- #form_filter(name, &block) ⇒ Object
- #form_messages(arg) ⇒ Object
Class Method Details
.registered(app) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/sinatra/formkeeper.rb', line 117 def self.registered(app) app.helpers Helpers app.set :form_validator, ::FormKeeper::Validator.new app.set :form_respondent, ::FormKeeper::Respondent.new app.set :form_failure_messages, nil app.before do reset_form end end |
Instance Method Details
#form_constraint(name, &block) ⇒ Object
81 82 83 84 |
# File 'lib/sinatra/formkeeper.rb', line 81 def form_constraint(name, &block) ::FormKeeper::Validator.register_constraint name, ::FormKeeper::Constraint::Custom.new(block) end |
#form_filter(name, &block) ⇒ Object
76 77 78 79 |
# File 'lib/sinatra/formkeeper.rb', line 76 def form_filter(name, &block) ::FormKeeper::Validator.register_filter name, ::FormKeeper::Filter::Custom.new(block) end |
#form_messages(arg) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/sinatra/formkeeper.rb', line 86 def (arg) case arg when String = ::FormKeeper::Messages.from_file(arg) when Hash = ::FormKeeper::Messages.new(arg) else raise ArgumentError.new end set :form_failure_messages, end |