When implementing a form with rails, I tried to register business hours using `` `time_field```, but validation did not work and I had a hard time, so I will keep it as a record.
In order to keep the order that I was addicted to, I will describe it in three steps.
<div class="time-field">
<%= f.time_select :open_time %> 〜
<%= f.time_select :close_time %>
</div>
Since the current time is displayed as it is, validation will pass even if you forget the setting.
<div class="time-field">
<%= f.time_select :open_time, prompt: true %> 〜
<%= f.time_select :close_time, prompt: true %>
</div>
If you haven't selected the time with this, it should be played by validation and an error will be displayed! I thought, but no error is displayed.
ignore_date: true
console
"open_time(1i)"=>"1", "open_time(2i)"=>"1", "open_time(3i)"=>"1", "open_time(4i)"=>"", "open_time(5i)"=>""
The first half " open_time (1i) "=>" 1 "," open_time (2i) "=>" 1 "," open_time (3i) "=>" 1 "
is the year, month, day If you do not enter the data, the value "1" will be entered and sent, so validation can be passed.
Add the ignore_date
option to avoid this situation
By setting it to true, if it is not entered, it will not be sent and it will be judged only by the time.
#### **`console`**
```ruby
"open_time(4i)"=>"", "open_time(5i)"=>"", "close_time(4i)"=>"", "close_time(5i)"=>""
The date part was not included in the parameters and the error message could be displayed safely.
Recommended Posts