[Rails] Put the same restrictions on multiple pieces of information

This article is based on the Rails documentation. https://railsdoc.com/validation#validates_format_of

with_options By writing with_options 〇〇 do ~ end, it is possible to add the same options to multiple pieces of information at once.

format option

This option is available when using the validates method in a model class. ** format: {with: regular expression, message: error message when the regular expression is not matched} ** By doing so, you can limit a specific column using a regular expression.

As for regular expressions, the basic thing is, "If you have this, you'll be fine for the time being." Please refer to it if you like! https://qiita.com/teipoi/items/8a28a56bc584780b8d8a


In the following, three validations can be restricted to "presence". In addition, the format option is used to set validation with regular expressions for names and reading kana.

apps/model/user.rb


with_options presence: true do
  validates :name, format: { with: /\A[Ah-Hmm-One-龥]/, message: "is invalid.
                             Input full-width characters." }
  validates :age, format: { with: /\A[0-9]+\z/, message: "is invalid. 
                            Input half-numbers." }
  validates :nickname, format: { with: /\A[a-z0-9]+\z/i, message: "is invalid.
                                 Input half-width characters." }
end

Recommended Posts

[Rails] Put the same restrictions on multiple pieces of information
part of the syntax of ruby ​​on rails
[Ruby on Rails] Until the introduction of RSpec
Docker the development environment of Ruby on Rails project
[Rails] Put together the same code with controller actions
Try using the query attribute of Ruby on Rails
[Rails] Register by attribute of the same model using Devise
(Ruby on Rails6) Display of the database that got the id of the database
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
How to run React and Rails on the same server
[Ruby on Rails] Change the save destination of gem refile * Note
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
[Rails] How to put a crown mark on the ranking function
[Rails] List instances of multiple models
[Rails] Check the contents of the object
Basic knowledge of Ruby on Rails
Explanation of the order of rails routes
Note on the path of request.getRequestDispatcher
Put a badge on the icon
Check the migration status of rails
Check the root on the Rails browser
Perform a large amount of csv export (output) of log information etc. on the WEB application with Rails application
[Register multiple photos] Register multiple rails images at the same time Primitive power technique
Put Zabbix in Ubuntu with Docker and monitor Docker on the same host
How to solve the local environment construction of Ruby on Rails (MAC)!
Ruby on Rails When you don't know the cause of rollback when saving.
[Ruby On Rails] How to search the contents of params using include?
[Rails] Addition of Ruby On Rails comment function
Display text on top of the image
Rails: 7 basic actions defined on the controller
The identity of params [: id] in rails
Samshin on the value of the hidden field
Let's summarize "MVC" of Ruby on Rails
[Rails 6.0] About batch saving of multiple records
[Rails] Change the label name of f.label
Looking back on the basics of Java
[Ruby on Rails] Japanese notation of errors
Explanation of Ruby on rails for beginners ①
The process of introducing Vuetify to Rails
[Ruby on rails] Implementation of like function
Find the difference from a multiple of 10
[Ruby on Rails] How to make the link destination part of the specified id
True / false judgment based on the condition of each difference with multiple integers
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Ruby On Rails] How to retrieve and display column information of multiple records linked to a specific id at once