Validations without save

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

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

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

Thorsten Mueller 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

Craig Demyanovich 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?

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 ], which has a "Create Multi-Step Wizards" recipe.

Regards, Craig

Craig Demyanovich wrote: