[RUBY] I want to eliminate duplicate error messages

Duplicate error message ①

Using the form_with method I made a form to input a numerical value in form.number_field. If you enter the correct number, no error will occur, and if you try to enter a number that exceeds the validation, an error will occur at that timing. However, if you send the input form empty,

Please enter ○○ ○○ is an invalid value ○○ is not in the list

And I get 3 errors. Just send the sky and you don't need three! !! !!



with_options presence: true do
    validates :grade, format: { with: /\A[0-9]+\z/, allow_blank: true}, inclusion: { in: 1..7, allow_blank: true }
    validates :class_number, format: { with: /\A[0-9]+\z/, allow_blank: true }, inclusion: { in: 1..10, allow_blank: true }

format is regular expression validation. inclusion is the validation of the numerical range.

allow_blank: true is a method that does not perform validation if the value is empty. It's verbose, but when it's empty, format and inclusion validation doesn't respond.

Looking at the error message again, "○○ is an invalid value" is responding to format It can be seen that "○○ is not in the list" is responding to inclusion.

Duplicate error message ②

If you use a foreign key and validate it with presence: true, you will get two error messages with similar content.


Delete the validation of the foreign key described in the model.

[Digression] I want to change the order of error messages

The order can be resolved by changing the description position of validation and association. I am grateful that no particular problem will occur because I only change the position of the description. This is probably because the code is being read from above. It's a little bit, but I want to send an error message in the order of the input form.

Recommended Posts

I want to eliminate duplicate error messages
I want to convert characters ...
Swift: I want to chain arrays
I want to use FormObject well
I want to convert InputStream to String
I want to docker-compose up Next.js!
I want to control the default error message of Spring Boot
I want to develop a web application!
I want to write a nice build.gradle
I want to make an ios.android app
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to RSpec even at Jest!
I want to write a unit test!
[Rails] How to display error messages individually
I want to install PHP 7.2 on Ubuntu 20.04.
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I want to target static fields to @Autowired
I want to do team development remotely
How to display error messages in Japanese
I want to display an error message when registering in the database
I want to test Action Cable with RSpec test
I want to sort by tab delimited by ruby
I want to output the day of the week
Run R from Java I want to run rJava
The road to Japaneseizing Rails devise error messages
[Ruby on Rails] How to display error messages
[Swift] I want to draw grid lines (squares)
I want to send an email in Java.
I want to graduate from npm install properly [2020]
I want to use arrow notation in Ruby
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
I want to simply write a repeating string
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
I want to play with Firestore from Rails
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
Convert error messages to Japanese Supports multiple tables
I want to write quickly from java to sqlite
I want to truncate after the decimal point
[Rails] How to get success and error messages
I want to reduce simple mistakes. To command yourself.
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
I want to delete files managed by Git
I want to get the value in Ruby
I want to morphologically analyze the log in the DB and put it in the DB to classify messages 1
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
I want to call a method of another class
I want to do something like "cls" in Java
I want to use NetBeans on Mac → I can use it!
I want to embed any TraceId in the log
Pointcut Expression I want to specify more than one
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)