# Rails validation scheme: why not validate params directly

**URL:** <https://www.ruby-forum.com/t/rails-validation-scheme-why-not-validate-params-directly/70460>\
**Category:** Rails\
**Created:** [September 29, 2006, 11:32am UTC](https://www.ruby-forum.com/t/rails-validation-scheme-why-not-validate-params-directly/70460 "2006-09-29T11:32:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [September 29, 2006, 11:32am UTC](https://www.ruby-forum.com/t/rails-validation-scheme-why-not-validate-params-directly/70460/1 "2006-09-29T11:32:55Z")

</div>

Hi. There are many ways of doing validation but I’m trying to think of  
the cleanest way without making the action code unclean. Would it be  
advisable to do validation on the incoming params hash rather than  
waiting to setup an object you are about to save and calling ojb.valid?  
before saving? Or even going as far as trying to save the object but  
failing from the model’s validation scheme?

Handling errors - How do people display errors cleanly? Do they use  
flash[:notice] in a try/catch block in the action to catch the  
validation exception?

Any help would be appreciated on designing this. I don’t want to mess  
up the actions with all the possible erorr messages etc… This jsut  
makes the code very unreadable.

Thanks,

Sam.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [September 29, 2006, 11:53am UTC](https://www.ruby-forum.com/t/rails-validation-scheme-why-not-validate-params-directly/70460/2 "2006-09-29T11:53:43Z")

</div>

On 29 Sep 2006, at 10:32, Sam D. wrote:

> Hi. There are many ways of doing validation but I’m trying to  
> think of  
> the cleanest way without making the action code unclean. Would it be  
> advisable to do validation on the incoming params hash rather than  
> waiting to setup an object you are about to save and calling  
> ojb.valid?  
> before saving? Or even going as far as trying to save the object but  
> failing from the model’s validation scheme?

Why validate params when your model knows better how to validate  
already?

The place for most validation is within the model. There is some  
validation which is better done at the controller level, so this  
isn’t an absolute rule. If validation fails, the model won’t be  
saved, so an additional check on valid? isn’t normally required.

> Handling errors - How do people display errors cleanly? Do they use  
> flash[:notice] in a try/catch block in the action to catch the  
> validation exception?

No, just conditionally display the flash within your normal view  
template, and set it from a check on your save result.

> Any help would be appreciated on designing this. I don’t want to mess  
> up the actions with all the possible erorr messages etc… This jsut  
> makes the code very unreadable.

You could get very tricky with checks on all sorts of things, but  
it’s usually better to go with the flow and just let Rails handle  
it. If you have followed the examples in the AWDwR book, you will  
have seen that this needn’t result in overcomplex code.

Paul
