Helper date_select. How validate dates 30 and 31 of february? - ruby-on-rails

I have model Freelancer and tables freelancers with property birthday, type date.
freelancers/edit.html.erb
<li class="form-fields__group form-fields__group_birthdate">
<label for="freelancer_birth_date">Birthday</label>
<div class= "field">
<%= f.date_select :birthday, { prompt: { day: 'Select day',
month: 'Select month',
year: 'Select year' },
include_blank: true, start_year: Time.now.year - 70},
{ class: 'form-control_medium'} %>
</div>
</li>
If I selcet february 30 or 31 return value 3 march. How me validate value date 30 and 31 if I selected february? Thank you

Related

How to add default dd/mm/yyyy label instead of null selector in rails?

Here is code for date selector in my rails form-
<div class="field columns large-3">
<%= form.label :planned_start_date, :class=>"required" %>
<%= form.date_select :planned_start_date, start_year: #project_start_year, end_year: #project_end_year, :include_blank => true, order: [:day, :month, :year], class: 'select-date' %>
</div>
<div class="field columns large-3">
<%= form.label :planned_end_date, :class=>"required" %>
<%= form.date_select :planned_end_date, start_year: #project_start_year, end_year: #project_end_year, :include_blank => true, order: [:day, :month, :year], class: 'select-date' %>
</div>
</div>
I want to add dd mm yyyy instead of null selector by default. How can i change that?
As mentioned in ruby doc for date_select
= form.date_select :planned_end_date, start_year: #project_start_year, end_year: #project_end_year, :include_blank => true, order: [:day, :month, :year], prompt: { day: 'dd', month: 'mm', year: 'yyyy' }, class: 'select-date'

Custom date_select values in Rails?

I would like to change the month select input from date_select : change the values of the array (with January, February... to Janvier, Février...). And I would also like to append styling to each select box (day, month and year). This is my date_select
<%= f.date_select :birthdate,
order: [:day, :month, :year],
prompt: { day: 'Jour', month: 'Mois', year: 'Année' },
start_year: Date.today.year - 13,
end_year: Date.today.year - 100
%>
I don't seem to find anything on the rails official documentation. Any help? Thanks :)
I guess below code would help you.
<%= f.date_select :birthdate,
{
order: [:day, :month, :year],
prompt: { day: 'Jour', month: 'Mois', year: 'Année' },
start_year: Date.today.year - 13,
end_year: Date.today.year - 100,
locale: :fr
},
{class: 'custom-style'}
%>
I guess setting f.date_select ... locale: :fr will help
Use the use_month_names option to set custom values for the months:
<%= f.date_select :birthdate,
{
order: [:day, :month, :year],
prompt: { day: 'Jour', month: 'Mois', year: 'Année' },
start_year: Date.today.year - 13,
end_year: Date.today.year - 100,
use_month_names: ["Janvier", "Février", etc..]
},
{ class: "some-css-class-for-styling" }
%>

ruby on rails year select issue

my code looks like this
<div class="field form-group">
<%= f.label :date_of_birth %><br />
<%= date_select :date_of_birth, {order: [:month, :day, :year],
prompt: { day: 'Select day', month: 'Select month', year: 'Select year' },
start_year:1950, end_year: Date.today.year}, {required: true}, class: "form-control" %>
</div>
And result is in this photo
What i want that year selection be from 1950 to now.
Thanks for help!
The following code works perfectly for me:
<%= f.date_select( :date_of_birth, { :order => [:month, :day, :year], :start_year => 1950, :end_year => Date.today.year }, { :required => true, :class => "form-control" }) %>
Note that my date_select control is prepended with f, for the form it belongs to... I think that might be your problem.

Order year in rails date_select

f.date_select(:birthday, prompt: {
day: 'Select day', month: 'Select month', year: 'Select year'
}, end_year: Time.now.year-13, start_year: 1900, required: true)
It shows start_year first and end_year last:
1900
1901
..
..
2002
I want 2002 first and 1900 at the bottom of the select box.
Couldn't find anything in rails documentation and google. Any help will be appreciated. Thanks
Simply exchanging end_year and start_year will do the trick.
In your case:
f.date_select(:birthday, prompt: {
day: 'Select day', month: 'Select month', year: 'Select year'
}, end_year: 1900, start_year: Time.now.year-13, required: true)

How to adjust date_select / time_select field size

I have been attempting to adjust the size of my form fields. I have tried using size like so...
<%= f.date_select(:form_date, order: [:month, :day, :year], prompt: { month: 'Select Month', day: 'Select Day', year: 'Select Year' }, start_year: 2000, size: 1) %>
Am I just entering size in the wrong order (size: 1 shouldn't be last)? Is this best handled in my stylesheets?
Thanks in advance.
Turns out I was mixing options and html_options
date_select(object_name, method, options = {}, html_options = {}) Link
Here is what I changed my line of code to from above...
<%= f.date_select(:form_date, options = { order: [:month, :day, :year], prompt: { month: 'Select Month', day: 'Select Day', year: 'Select Year' }, start_year: 2000 }, html_options = { size: 1 }) %>

Resources