Setting Up Data Validation

Your Rails model can include validation that helps to keep invalid or inconsistent data out of the database. Track Magic can help you add validation code to your model.

Opening the Rails Model

There are two ways to open the Rails model. If the Panorama database associated with this table is open you can choose Open Model from the Sources submenu of the Rails menu.

001_OpenModelMenu

You can also open the model directly from the Track Magic window. Select the table, then choose Open Model from the Table menu.

002_OpenModelMenu

Both methods open a simple editor window for the model.

003_Model

The Validate Menu

The Validate menu appears when the model editor window is open.

004_ValidateMenu

This menu allows you to open seven different dialogs for adding new validation options to the model. Note: These dialogs simply add new validations to the model. It is up to you to make sure that these new validations don’t conflict with existing validations already in the model.

Each of these seven dialogs starts a pop-up menu for selecting the field to be validated. You can only add validations one field at a time.

Each dialog allows you to specify an error message that will be used if this validation fails (in some cases multiple messages may be specified for different types of validation failures). This message is optional — if left blank, Rails will use the default error message for this type of validation.

The other options are specific to the type of validation being set up. Each dialog has a ? button that will open a web page with detailed information about this type of validation.

Presence_of

This type of validation simply prevents the user from leaving the specified field blank.

005_ValidatePresenceOf

When you press the Add Validation button the validation code will be automatically inserted into the model. Of course you can edit this code further if you like, using either the built-in editor window or an external editor like TextMate.

Length_of

This validation prevents the user from entering data that is too short and/or too long.

006_ValidateLengthOf

Numericality_of

This validation prevents the user from entering numbers that are two large or two small, or that don’t meet some other numeric specification (non-integer, even, odd, etc.)

007_ValidateNumber

Format_of

This validation prevents the user from entering data that fails to match a regular expression. You can enter your own custom regular expression or pick from the pop-up menu of regular expressions for common data formats.

008_ValidateFormatOf

Here's an example of a regular expression for validating a fields format (in this case, a phone number).

008a_ValidateFormatOf

Uniqueness_of

This validation prevents the user from entering duplicate data into the database. The scope pop-up menus allow you to specify that the data value must be unique across multiple fields.

009_ValidateUniquenessOf

Inclusion_of

This validation prevents the user from entering values that are not specifically listed. For example you might want to restrict a gender field to M or F, or a state field to a list of actual states (AK, AR, AZ, etc.). The allowable values should be listed one per line. (You can also specify a range of allowable values, for example 30..60.)

010_ValidateInclusionOf

Exclusion_of

This validation prevents the user from entering values that are specifically listed. The non-allowed values should be listed one per line. (You can also specify a range of non-allowed values, for example 18..21.)

011_ValidateExclusionOf