[RAILS] How to specify validation for time_field


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.

  1. Generate a time selection box with no options
    <div class="time-field">
      <%= f.time_select :open_time %> 〜
      <%= f.time_select :close_time %>

204b83a9e02c5af89386534b8d0aeac4.png Since the current time is displayed as it is, validation will pass even if you forget the setting.

  1. Add `` `prompt: true``` as an option
    <div class="time-field">
      <%= f.time_select :open_time, prompt: true %> 〜
      <%= f.time_select :close_time, prompt: true %>

be7a44a3360206f251ae9e27117a9c94 (1).png 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.

  1. Add ignore_date: true
    The reason why the validation does not play even though the time should not be specified is that the date information is also sent. Checking the parameters when executing the create action, it is as follows


"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`**

"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

How to specify validation for time_field
[Validation] rails How to specify after today's date
How to specify index of JavaScript for statement
How to write Rails validation
[Rails] How to use validation
How to install JMeter for Mac
How to use binding.pry for view files
How to install Play Framework 2.6 for Mac
How to create a Maven repository for 2020
[Ruby] How to use slice for beginners
How to implement TextInputLayout with validation function
Option to specify validation execution timing [on:]
Needed for iOS 14? How to set NSUserTrackingUsageDescription
How to specify id attribute in JSF
[For beginners] How to debug in Eclipse
How to deploy
[Java] How to test for null with JUnit
How to create a database for H2 Database anywhere
How to find OSS trends for web development
How to use an array for HashMap keys
How to create pagination for a "kaminari" array
[Rails] How to implement unit tests for models
[For beginners] How to implement the delete function
How to make a lightweight JRE for distribution
[Java] (for MacOS) How to set the classpath
[Rails] How to use Gem'rails-i18n' for Japanese support
How to use nginx-ingress-controller with Docker for Mac
[For super beginners] How to use autofocus: true
[Java] How to make multiple for loops single
How to implement Pagination in GraphQL (for ruby)
How to implement login request processing (Rails / for beginners)
How to develop OpenSPIFe
How to set Dependency Injection (DI) for Spring Boot
[Rails] How to create a signed URL for CloudFront
How to build Docker + Springboot app (for basic learning)
How to call AmazonSQSAsync
How to use Map
How to write Rails
How to use rbenv
How to write a unit test for Spring Boot 2
How to build docker environment with Gradle for intelliJ
Rails / Ruby: How to get HTML text for Mail
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to deal with No template for interactive request
[For beginners] How to operate Stream API after Java 8
How to use collection_select
[Spring Boot] How to create a project (for beginners)
How to adapt Bootstrap
How to use Twitter4J
How to use active_hash! !!
How to install Docker
How to use MapStruct
How to use hidden_field_tag
[SwiftUI] How to specify the abbreviated position of Text
How to use TreeSet
How to write dockerfile
How to use Truth (assertion library for Java / Android)