public
Description: Rails plugin: a custom, template-based form builder that attempts to make your forms friendly (and maybe even fun!).
Homepage: http://rpheath.com/posts/376-form-assistant-plugin-updates
Clone URL: git://github.com/rpheath/form_assistant.git
added ability to mark a field as required
rpheath (author)
Thu Dec 11 05:04:00 -0800 2008
commit  968f52d454d52ae6b09171f2c9006f9573d12fbf
tree    9d69f5b1b8bdbf9957d72d80a73035d7cd74f7e1
parent  3b09223f7c33a34a40f638f931d6bd6b2bfd92d0
...
73
74
75
 
 
 
 
 
 
 
 
76
77
78
...
73
74
75
76
77
78
79
80
81
82
83
84
85
86
0
@@ -73,6 +73,14 @@ There's also a @#fieldset()@ helper available to you, although it doesn't belong
0
 
0
 The nice thing about that is it's also controlled by a template (cleverly called '_fieldset.html.erb').
0
 
0
+h3. Required Fields
0
+
0
+You can now pass a @:required@ flag to the field helpers, and it will be available within the templates.
0
+
0
+<pre><code><%= form.text_field :title, :required => true %></code></pre>
0
+
0
+Then you can check the 'required' local variable and handle it accordingly. Naturally, this defaults to false.
0
+
0
 h3. Form Labels
0
 
0
 Another convenient thing about the form assistant is the ability to control labels from their respective helper. For example...
...
85
86
87
88
 
89
90
 
91
92
93
...
153
154
155
 
 
 
156
157
158
...
160
161
162
163
 
164
165
166
...
85
86
87
 
88
89
 
90
91
92
93
...
153
154
155
156
157
158
159
160
161
...
163
164
165
 
166
167
168
169
0
@@ -85,9 +85,9 @@ module RPH
0
       
0
     protected
0
       # renders the appropriate partial located in the template root
0
-      def render_partial_for(element, field, label, tip, template, helper, args)
0
+      def render_partial_for(element, field, label, tip, template, helper, required, args)
0
         errors = self.class.ignore_errors ? nil : error_message_for(field)
0
-        locals = { :element => element, :label => label, :errors => errors, :tip => tip, :helper => helper }
0
+        locals = { :element => element, :label => label, :errors => errors, :tip => tip, :helper => helper, :required => required }
0
 
0
         @template.render :partial => "#{self.class.template_root}/#{template}", :locals => locals
0
       end
0
@@ -153,6 +153,9 @@ module RPH
0
           # grab the tip, if any
0
           tip = options.delete(:tip)
0
           
0
+          # is the field required?
0
+          required = !!options.delete(:required)
0
+          
0
           # call the original render for the element
0
           element = super(field, *(args << options))
0
           
0
@@ -160,7 +163,7 @@ module RPH
0
           return render_element(element, field, name, options, ignore_label) if self.class.ignore_templates
0
           
0
           # render the partial template from the desired template root
0
-          render_partial_for(element, field, label, tip, template, name, args)
0
+          render_partial_for(element, field, label, tip, template, name, required, args)
0
         end
0
       end
0
       

Comments