# Validations without save

**URL:** <https://www.ruby-forum.com/t/validations-without-save/142212>\
**Category:** Rails\
**Created:** [July 4, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/validations-without-save/142212 "2008-07-04T17:17:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 4, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/validations-without-save/142212/1 "2008-07-04T17:17:15Z")

</div>

the validates\_format and other validations are called when the .save  
function is called on an object.

If I want the validations to be called without saving is there a way to  
do this?  
Do I user .create?

Thank you in advance for your time.

Mitch

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 4, 2008, 5:29pm UTC](https://www.ruby-forum.com/t/validations-without-save/142212/2 "2008-07-04T17:29:52Z")

</div>

If you just want to know whether a model is valid, ask it:  
model.valid? That will run the validations, populate model.errors and  
return true/false. Is there something else that you want to do?

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 4, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/validations-without-save/142212/3 "2008-07-04T17:32:47Z")

</div>

You can just create a new object

@user = User.new(params[:user])

and then check with

@user.valid?

a User.create would create, validate and save in one go,  
what’s exactly what you wanted to avoid

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 4, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/validations-without-save/142212/4 "2008-07-04T17:53:22Z")

</div>

Craig D. wrote:

> If you just want to know whether a model is valid, ask it:  
> model.valid? That will run the validations, populate model.errors and  
> return true/false. Is there something else that you want to do?
> 
> Regards,  
> Craig

Thanks Craig that answers my question.  
I would do in the user controller

def registration

If model.valid?

redirect\_to :action =\> ‘user\_details’

end

#otherwise it will go back to the user registration page automatically?  
#The form will show the error\_messages\_for :user correct?  
end

something like that look about right?

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 4, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/validations-without-save/142212/5 "2008-07-04T17:48:32Z")

</div>

Thorsten M. wrote:

> You can just create a new object
> 
> @user = User.new(params[:user])
> 
> and then check with
> 
> @user.valid?
> 
> a User.create would create, validate and save in one go,  
> what’s exactly what you wanted to avoid

Thank you very much Thorsten that will do the trick.

Mitch

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 4, 2008, 6:38pm UTC](https://www.ruby-forum.com/t/validations-without-save/142212/6 "2008-07-04T18:38:30Z")

</div>

On Fri, Jul 4, 2008 at 11:53 AM, Mitchell G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #otherwise it will go back to the user registration page automatically?  
> #The form will show the error\_messages\_for :user correct?  
> end
> 
> something like that look about right?

Things like the redirect to the user registration page won’t happen  
automatically because you call model.valid?; you’ll have to make it  
happen by putting it in a branch of your conditional logic, for  
example. Also, while the error messages will be displayed in the view  
if you use the error\_messages\_for helper, the helper may display an  
empty but colored div if there are no errors, and you probably don’t  
want that. That’s also something that you’ll have to try and then  
handle however you want to handle it yourself.

If you’re doing a multi-step process, you might want to check out  
\_Advanced Rails Recipes \_ [  
[http://pragprog.com/titles/fr\_arr/advanced-rails-recipes](http://pragprog.com/titles/fr_arr/advanced-rails-recipes) ], which has  
a “Create Multi-Step Wizards” recipe.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [July 4, 2008, 6:56pm UTC](https://www.ruby-forum.com/t/validations-without-save/142212/7 "2008-07-04T18:56:18Z")

</div>

Craig D. wrote:

> On Fri, Jul 4, 2008 at 11:53 AM, Mitchell G.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > #otherwise it will go back to the user registration page automatically?  
> > #The form will show the error\_messages\_for :user correct?  
> > end
> > 
> > something like that look about right?
> 
> Things like the redirect to the user registration page won’t happen  
> automatically because you call model.valid?; you’ll have to make it  
> happen by putting it in a branch of your conditional logic, for  
> example. Also, while the error messages will be displayed in the view  
> if you use the error\_messages\_for helper, the helper may display an  
> empty but colored div if there are no errors, and you probably don’t  
> want that. That’s also something that you’ll have to try and then  
> handle however you want to handle it yourself.
> 
> If you’re doing a multi-step process, you might want to check out  
> \_Advanced Rails Recipes \_ [  
> [http://pragprog.com/titles/fr\_arr/advanced-rails-recipes](http://pragprog.com/titles/fr_arr/advanced-rails-recipes) ], which has  
> a “Create Multi-Step Wizards” recipe.
> 
> Regards,  
> Craig

Thats very helpful. I really appreciate the info and will check it out.

thanks again!

MItch
