On Feb 24, 3:27pm, Valery K. [email protected] wrote:
Interesting question.
Instead of ‘date_select’, you can use ‘select_date’ (render just the tag), in
this case date parameters will be named as you want. Just write the accessors and
attribute assignment logic, then put the custom validator and this is it.
As for me, I completely gave up date_helper and other selectors, and began to
use strings for dates in a form, adding to their Jquery
I also used strings, as it is much easier to enter a date with text
than picking each field from the dropdowns. I made it so that if you
enter a full year, fine. But if you do a 1 or 2 digit year it will
pick the rest of the year correctly. If you have 130 year old dates
you will have to enter all 4 digits.
here’s the code
model>
before_validation :b4_valid
def b4_valid
self.xmonth = self.month_.to_i
self.xday = self.day_.to_i
self.xyear = self.year_.to_i
begin
if (self.xyear >= 0 && self.xyear <= 100)
@compare = Date.new(2000+self.xyear, 1,1)
if (@compare > Date.today)
self.xyear = 1900+self.xyear
self.year_ = self.xyear.to_s
else
self.xyear = 2000+self.xyear
self.year_ = self.xyear.to_s
end
end
rescue
return false
end
begin
self.birthday = Date.new(self.xyear, self.xmonth, self.xday)
rescue
return false
end
end
view>
<% fields_for “household[people_attributes][]”, person do |
person_form| %>
<% fields_for "household" do |household_form| %>
<%= household_form.radio_button :hoh, person.hoh %>
<% end %>
<%= person_form.hidden_field :hoh, :index => nil %>
<%= person_form.text_field :last_name, :style => 'text-align:
left', :class => 'last_name', :size => 25, :maxlength => 25, :index =>
nil, :autocomplete => "off" %>
<%= person_form.text_field :first_name, :style => 'text-align:
left', :class => 'first_name', :size => 25, :maxlength =>25, :index =>
nil, :autocomplete => "off" %>
<%= person_form.text_field :middle, :style => 'text-align:
right', :class => 'middle', :size => 1, :maxlength =>1, :index =>
nil, :autocomplete => "off" %>
<%= person_form.text_field :sex, :style => 'text-align:
right', :size => 1, :maxlength =>1, :index => nil, :autocomplete =>
"off" %>
<%= person_form.text_field :month_, :style => 'text-align:
right', :size => 2, :maxlength =>2, :index => nil, :autocomplete =>
"off" %>
<%= person_form.text_field :day_, :style => 'text-align:
right', :size => 2, :maxlength =>2, :index => nil, :autocomplete =>
"off" %>
<%= person_form.text_field :year_, :style => 'text-align:
right', :size => 4, :maxlength =>4, :index => nil, :autocomplete =>
"off" %>
<% if !person_form.object.new_record? %>
<%= person_form.hidden_field :id, :index => nil %>
<%= link_to 'Delete', person_path(person.id), :confirm => 'Are you
sure?', :method => :delete %>
<% end %>
<% end %>
This will calculate the date object based on the input and fail
validation if the date is incorrect. Because of the rescue statements,
there should be no errors that cause the program to abort. All errors
will show as validation failures.
Best of luck
Bob [email protected]