Comparison of values: Validations

Hi,

I wanted to know where validations placed are most effective: Model, View or controller.

In my application i enter data in a text box and if it is greater than a given value (which varies depending on choice), it should throw an error message and data shouldn't be saved.

Thanks, Premanshu

You've answered your own question here. The Model is responsible for saving data back to your persistence store (whatever that is) so that is the logical place for validations to live. More than one controller can access and send data to your one and only model, they should never repeat your validations there (DRY).

Walter

Hi,

I wanted to know where validations placed are most effective: Model,

View or controller.

Always in the models. You can do client side (browser Javascript) validations but those should only be in addition to server side for security. Controllers should be kept as lean as possible and used for directing traffic, not “business logic” such as validations.

See: http://edgeguides.rubyonrails.org/3_0_release_notes.html