Class: FormWidget
- Inherits:
-
Cuca::Widget
- Object
- Cuca::Widget
- FormWidget
- Defined in:
- lib/cuca/stdlib/form.rb
Overview
FormWidget
To implement a form inherit this class and overwrite:
-
form - generate your form (as if it was the output method of a widget)
-
Use @form_name for your html form name
-
Use @submit_name for your submit button
-
-
validate - to validate a posted form
-
write @form_errors on errors (to handle them with FormErrors Widget)
-
-
setup - Define initial values for your form
A form will call form_name_submit(result) on the CONTROLLER if the form was submitted and validation passed. If you don’t want this, overwrite on_submit
A form will use instance variables for form values
Direct Known Subclasses
Instance Method Summary collapse
-
#form ⇒ Object
Create your form by overwriting this demo Name your submit button @submit_name, so the form can detect if it is submitted or not.
-
#get_variables ⇒ Object
get from params and return them as hash.
-
#load_variables ⇒ Object
get from params and set instance variables.
-
#on_submit ⇒ Object
If form is validated we call on_submit.
- #output(form_name, post_to = nil) ⇒ Object
- #posted? ⇒ Boolean
-
#setup ⇒ Object
Overwrite this method to setup initial values This method will not be called if the form get submitted.
-
#validate ⇒ Object
Overwrite this method with your validation code.
Methods inherited from Cuca::Widget
#app, #cgi, #clear, clear_hints, #content, #content=, #controller, define_attr_method, #escape, #escapeHTML, #get_assigns, #hints, #initialize, #log, #params, #query_parameters, #request_method, #request_parameters, run_attr_method, #session, #to_s, #unescape, #unescapeHTML
Constructor Details
This class inherits a constructor from Cuca::Widget
Instance Method Details
#form ⇒ Object
Create your form by overwriting this demo Name your submit button @submit_name, so the form can detect if it is submitted or not.
44 45 |
# File 'lib/cuca/stdlib/form.rb', line 44 def form end |
#get_variables ⇒ Object
get from params and return them as hash
29 30 31 32 33 |
# File 'lib/cuca/stdlib/form.rb', line 29 def get_variables r = {} params.each_pair { |k,v| r[k] = v } return r end |
#load_variables ⇒ Object
get from params and set instance variables
24 25 26 |
# File 'lib/cuca/stdlib/form.rb', line 24 def load_variables params.each_pair { |k,v| instance_variable_set('@'+k, v) } end |
#on_submit ⇒ Object
If form is validated we call on_submit. Default behaviour is to call form_name_submit(result) on the CONTROLLER.
54 55 56 |
# File 'lib/cuca/stdlib/form.rb', line 54 def on_submit controller.send(@form_name+'_submit', get_variables) unless controller.nil? end |
#output(form_name, post_to = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/cuca/stdlib/form.rb', line 58 def output(form_name, post_to = nil) @form_name = form_name @post_to = post_to || cgi.path_info @submit_name = 'submit_'+@form_name @form_errors = {} if posted? then load_variables validate if @form_errors.empty? then clear # submitted forms should not have any content that might have been generated return on_submit else form end else setup form end end |
#posted? ⇒ Boolean
19 20 21 |
# File 'lib/cuca/stdlib/form.rb', line 19 def posted? return (request_method == 'POST' && !params[@submit_name].nil?) end |
#setup ⇒ Object
Overwrite this method to setup initial values This method will not be called if the form get submitted.
37 38 |
# File 'lib/cuca/stdlib/form.rb', line 37 def setup end |
#validate ⇒ Object
Overwrite this method with your validation code
49 50 |
# File 'lib/cuca/stdlib/form.rb', line 49 def validate end |