Validating email address by defining custom validations in ruby on rails.
This post is more about how to define a custom validation in ruby on rails. I am explaining the same by adding a custom validation for email.
Before:
Result we want I want to write something like this:
Here is how to do something like this:
Step 1:
Create a new file, i.e., email_format_validator.rb in the lib directory and paste following code there:
Step 2:
Define error message, i.e., Paste this sample code in config/locales/en.yml
Step 3: Auto load validator file, i.e., pastes these two lines in application.rb under config directory.
Thats it. Now restart your application. Your custom validator is defined :)