Validations
Explore how to implement validations in Ruby on Rails models to enforce rules like presence and minimum length. Understand how to handle save actions conditionally in controllers and display error messages in views to inform users about invalid inputs, ensuring your application maintains data integrity.
We'll cover the following...
If you start up the application now and create a new pet, you will notice that you can provide any input to each of the fields. You can even leave them all blank and a new pet will still be created.
You want to impose certain restrictions on what you consider a valid pet. For example, a pet must have a name, and its description must exceed a minimum character count. To impose these restrictions, we use validations.
Validations
Adding validation to model
To add validation into your application you will open the app/models/pet.rb file and add the following lines to your Pet model class: