[Rails] How to use validation

I learned about validation, so As an output, we will introduce the features and usage of validation.

What is validation?

What is validation? "When a parameter is sent from the view to the server side through the input form, it is a function to verify whether it is a normal value".

The main use case is to verify the contents of the form and check it. If not, an error is displayed.

For example, if you require the user to enter the phone number.

validates :e-mail, presence: true

This will allow you to see the error if no e-mail has been entered.

Let's reduce the code with validates_presence_of

If you want to check if the value is empty as I raised earlier, You can use validates_presence_of for further simplification.

Example


validates_presence_of :e-mail: 

Now you can return an error if no e-mail has been entered.

Recommended Posts

[Rails] How to use validation
[Rails] How to use enum
How to use rails join
How to write Rails validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
How to use Ruby on Rails
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Map
How to use custom helpers in rails
How to write Rails
How to use rbenv
[Ruby on Rails] How to use CarrierWave
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
[Rails] How to use rails console with docker
How to use map
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use MySQL in Rails tutorial
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
How to use Dozer.mapper
How to use Gradle
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use credentials.yml.enc introduced in Rails 5.2
How to use Map
[Rails] How to use select boxes in Ransack
How to use rails g scaffold, functions, precautions
[Validation] rails How to specify after today's date
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
[Ruby on Rails] How to use session method
[Rails] How to use PostgreSQL in Vagrant environment
[Java] How to use Map
How to use Chain API
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return