Skip to content
Browse files

added ability to mark a field as required

  • Loading branch information...
1 parent 3b09223 commit 968f52d454d52ae6b09171f2c9006f9573d12fbf @rpheath rpheath committed Dec 11, 2008
Showing with 14 additions and 3 deletions.
  1. +8 −0 README.textile
  2. +6 −3 lib/form_assistant.rb
View
8 README.textile
@@ -73,6 +73,14 @@ There's also a @#fieldset()@ helper available to you, although it doesn't belong
The nice thing about that is it's also controlled by a template (cleverly called '_fieldset.html.erb').
+h3. Required Fields
+
+You can now pass a @:required@ flag to the field helpers, and it will be available within the templates.
+
+<pre><code><%= form.text_field :title, :required => true %></code></pre>
+
+Then you can check the 'required' local variable and handle it accordingly. Naturally, this defaults to false.
+
h3. Form Labels
Another convenient thing about the form assistant is the ability to control labels from their respective helper. For example...
View
9 lib/form_assistant.rb
@@ -85,9 +85,9 @@ def template_exists?(template)
protected
# renders the appropriate partial located in the template root
- def render_partial_for(element, field, label, tip, template, helper, args)
+ def render_partial_for(element, field, label, tip, template, helper, required, args)
errors = self.class.ignore_errors ? nil : error_message_for(field)
- locals = { :element => element, :label => label, :errors => errors, :tip => tip, :helper => helper }
+ locals = { :element => element, :label => label, :errors => errors, :tip => tip, :helper => helper, :required => required }
@template.render :partial => "#{self.class.template_root}/#{template}", :locals => locals
end
@@ -153,14 +153,17 @@ def render_element(element, field, name, options, ignore_label = false)
# grab the tip, if any
tip = options.delete(:tip)
+ # is the field required?
+ required = !!options.delete(:required)
+
# call the original render for the element
element = super(field, *(args << options))
# return the helper with an optional label if templates are not to be used
return render_element(element, field, name, options, ignore_label) if self.class.ignore_templates
# render the partial template from the desired template root
- render_partial_for(element, field, label, tip, template, name, args)
+ render_partial_for(element, field, label, tip, template, name, required, args)
end
end

0 comments on commit 968f52d

Please sign in to comment.
Something went wrong with that request. Please try again.