Recall & Review
beginner
What is format validation in Rails?
Format validation checks if a model attribute matches a specific pattern, usually using regular expressions, to ensure data is in the correct form.
Click to reveal answer
beginner
How do you add a format validation to a Rails model attribute?
Use
validates :attribute, format: { with: /regex/, message: 'error message' } inside the model to enforce a pattern.Click to reveal answer
beginner
Why use format validation instead of just presence validation?
Presence validation only checks if a value exists, but format validation ensures the value follows a specific pattern, like an email or phone number format.
Click to reveal answer
intermediate
Example: Validate an email format in a Rails model.
Use
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, message: 'must be a valid email' } to check email format.Click to reveal answer
beginner
What happens if a format validation fails when saving a Rails model?
The model will not save, and the error message defined in the validation will be added to the model's errors, which can be shown to the user.
Click to reveal answer
Which method is used to add format validation in a Rails model?
✗ Incorrect
The correct syntax is
validates :attribute, format: { with: /regex/ }.What does the
with option specify in format validation?✗ Incorrect
The
with option defines the regex pattern the attribute must match.If a format validation fails, what happens when you try to save the model?
✗ Incorrect
The model will not save and will add error messages for the failed validations.
Which built-in regex can you use to validate emails in Rails?
✗ Incorrect
Rails provides
URI::MailTo::EMAIL_REGEXP as a standard email regex.Can format validation be used to check phone number patterns?
✗ Incorrect
Format validation works with any regex pattern, including phone numbers.
Explain how to add a format validation to a Rails model attribute and why it is useful.
Think about how you tell Rails to check a pattern for an attribute.
You got /4 concepts.
Describe what happens when a format validation fails during model saving in Rails.
Consider the flow of validation and saving.
You got /4 concepts.