142 Validations [1d]

Updated . Posted . Visible to the public.

Basic validations

Read the Rails Guide on ActiveRecord Validations Show archive.org snapshot . You should have an overview which kinds of validations are built into Rails.

Also read Testing ActiveRecord validations with RSpec.

Make the following changes to your MovieDB:

  • Make sure your MovieDB models have validations for all required fields.
  • Make sure you have specs for all your validations in MovieDB.
  • Use Shoulda matchers where you think they improve the readability your code.

Working with error messages

Understand how to use the #errors method Show archive.org snapshot to inspect the reasons why a record is invalid.

Make the following changes to your MovieDB:

  • There is no longer a full list of error messages at the top of each failed form.

  • When a form could not be saved, only the fields with errors show their respective error messages.

    Your forms should be triples of label, input and error message:

    Title
    [_____________________]
    Title must not be blank
    
    Release year
    [____-5]
    Release year must be a positive number
    
  • If a field has more than one error, only show the first error.

  • A web app has a lot of fields, and each must be able to display an error message. Make it easy for a form view to display the error message for a given record and field. Remove as much code from the view as possible.

  • Style your form so that when a field is invalid, the label and error message is colored red. In addition, an invalid field control should have a red border.

    Tip

    Rails adds an extra element around invalid inputs to help with styling. Inspect an invalid field in your browser to see how it works.

  • Add support for a positive and negative flash message Show archive.org snapshot in your application layout. When a form could be saved successfully, show a flash like "Movie saved". When a form is re-rendered due to a validation error, show a flash like "Could not save".

    Tip

    Unlike @variables set by a controller, flash messages persist to the next request. This is to support the case of setting a flash and then redirect.

    If you have trouble with flash messages appearing longer than they should, you can set them to flash.now or clear all flash messages after displaying them by using flash.clear.

Custom validations

Add the following feature to your MovieBD:

  • The release year field of Movies should always be a number. It can also be missing (nil).
  • Movies have a film type. This is a string that is either "black_and_white" or "color".
  • The default film type is "color".
  • In the movie form we can switch between black and white and color movies using a <select>.
  • Add a validation that ensures that release_year > 1895 for black and white movies and release_year > 1917 for color movies.

Add tests for your validation. Make sure you have a test for each case.

Henning Koch
Last edit
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2021-10-11 13:09)