When the built-in validations are not enough to meet specific needs, custom validations can be written. A custom validation is a private method in a model which adds an error if a validation condition is not met.
- [Narrator] I think you'll find…that the built-in set of active record validations…will meet most of your needs, most of the time.…However it's also possible to write custom validations…if they don't meet your needs.…It's actually very easy to do.…All you need to do is use the validate method,…followed by the name of another method,…which should be run for your validations.…So here I have validate custom_method.…Somewhere else in my model then,…I need to define that custom method…and that custom method is going to determine…whether or not this object is valid.…
Typically you want to make this method a private method.…That means that you would put it below…a line that says private.…Anything following private to be considered private method…and that means that it won't be accessible…outside of this class.…No person or code will be able to get to it…except for the current model.…The reason that validations fail…is because errors are present in the errors array.…That's all that valid question mark actually does.…It runs the validations,…
- Creating and configuring a new Ruby on Rails project
- Generating controllers and views
- Handling server requests
- Using different types of routes
- Rendering and viewing templates
- Generating migrations and models
- Creating, updating, and deleting records
- Finding records with queries
- Understanding relationship types
- Writing controllers for CRUD
- Working with layouts and helpers
- Managing application assets
- Building forms
- Validating data
- Authenticating users
Skill Level Beginner
1. What Is Ruby on Rails?
2. Get Started
3. Controllers, Views, and Dynamic Content
4. Databases and Migrations
5. Models and ActiveRecord
7. CRUD, REST, and Resourceful Routes
8. Controllers and CRUD
9. Layouts, Partials, and View Helpers
12. Data Validation
13. Controller Features
14. User Authentication
15. Improve the Simple CMS
Next steps1m 44s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.