# Validation in controllers

**URL:** <https://www.ruby-forum.com/t/validation-in-controllers/115342>\
**Category:** Rails\
**Created:** [September 24, 2007, 6:25am UTC](https://www.ruby-forum.com/t/validation-in-controllers/115342 "2007-09-24T06:25:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![raker\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@raker\_t](https://www.ruby-forum.com/u/raker_t)\
**Post date:** [September 24, 2007, 6:25am UTC](https://www.ruby-forum.com/t/validation-in-controllers/115342/1 "2007-09-24T06:25:32Z")

</div>

Hi All, I am working on my first RoR application and running into an  
issue. I’ve been working through the authentication example in the AWDR  
book and have a question about the login form. Basically, there is a  
login page that has an email address/password field on it. There is a  
login method that basically looks for the user by email address, if they  
exist, it attempts to authenticate them. My question is this: since I am  
not trying to save a new model, is there a way to take advantage of  
active record’s built-in validation? I’d like to be able to validate  
that the email/password are populated and that they are valid. I’d also  
like to be able to display the error message in the same sort of errors  
collection accessible by the model. I noticed I’m not able to access the  
‘errors’ collection from the controller. Any help would be appreciated.

Thanks in advance,

J

---

<div class="post-metadata">

**Author:** ![raker\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@raker\_t](https://www.ruby-forum.com/u/raker_t)\
**Post date:** [September 24, 2007, 6:35am UTC](https://www.ruby-forum.com/t/validation-in-controllers/115342/2 "2007-09-24T06:35:41Z")

</div>

The ActiveRecord validation is applied when you save a new instance of a  
model. For a login feature, you aren’t saving a new model (as you  
noted) and therefore it doesn’t apply here.

If you have some particular reason to validate the login credentials  
before pulling the user info from the database (why would you want to do  
that, though?), you could do a User.new(params).valid?, but that would  
give you errors for all of any empty fields you are validating in the  
model.

---

<div class="post-metadata">

**Author:** ![raker\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@raker\_t](https://www.ruby-forum.com/u/raker_t)\
**Post date:** [September 24, 2007, 6:39am UTC](https://www.ruby-forum.com/t/validation-in-controllers/115342/3 "2007-09-24T06:39:33Z")

</div>

Dave S. wrote:

> The ActiveRecord validation is applied when you save a new instance of a  
> model. For a login feature, you aren’t saving a new model (as you  
> noted) and therefore it doesn’t apply here.
> 
> If you have some particular reason to validate the login credentials  
> before pulling the user info from the database (why would you want to do  
> that, though?), you could do a User.new(params).valid?, but that would  
> give you errors for all of any empty fields you are validating in the  
> model.

The purpose is determine whether or not a i have a syntactically valid  
email address and that the email address and password are filled in  
before i go searching the database to see if the user is valid or not.  
Does that make more sense?

---

<div class="post-metadata">

**Author:** ![raker\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@raker\_t](https://www.ruby-forum.com/u/raker_t)\
**Post date:** [September 24, 2007, 7:21am UTC](https://www.ruby-forum.com/t/validation-in-controllers/115342/4 "2007-09-24T07:21:43Z")

</div>

> The purpose is determine whether or not a i have a syntactically valid  
> email address and that the email address and password are filled in  
> before i go searching the database to see if the user is valid or not.  
> Does that make more sense?

You could use some client-side javascript validation for that.

But if you want to do it server side, you would do something like…

# Assuming you params[:email] and params[:password]

u = User.new(params)  
u.valid?

email\_invalid, password\_invalid =  
u.errors.on(:email), u.errors.on(:password)

unless email\_invalid or password\_invalid  
user = User.find(:first, :conditions =\> [‘email = ?’, params[:email]])  
blah\_blah\_blah  
end

---

<div class="post-metadata">

**Author:** ![raker\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@raker\_t](https://www.ruby-forum.com/u/raker_t)\
**Post date:** [September 24, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/validation-in-controllers/115342/5 "2007-09-24T20:02:03Z")

</div>

Dave S. wrote:

> > The purpose is determine whether or not a i have a syntactically valid  
> > email address and that the email address and password are filled in  
> > before i go searching the database to see if the user is valid or not.  
> > Does that make more sense?
> 
> You could use some client-side javascript validation for that.
> 
> But if you want to do it server side, you would do something like…
> 
> # Assuming you params[:email] and params[:password]
> 
> u = User.new(params)  
> u.valid?
> 
> email\_invalid, password\_invalid =  
> u.errors.on(:email), u.errors.on(:password)
> 
> unless email\_invalid or password\_invalid  
> user = User.find(:first, :conditions =\> [‘email = ?’, params[:email]])  
> blah\_blah\_blah  
> end

Dave,

I think this should do the trick. Thank you for your help.

- J

---

<div class="post-metadata">

**Author:** ![raker\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@raker\_t](https://www.ruby-forum.com/u/raker_t)\
**Post date:** [September 25, 2007, 1:41am UTC](https://www.ruby-forum.com/t/validation-in-controllers/115342/6 "2007-09-25T01:41:56Z")

</div>

Jim Dix wrote:

> Dave S. wrote:
> 
> > > The purpose is determine whether or not a i have a syntactically valid  
> > > email address and that the email address and password are filled in  
> > > before i go searching the database to see if the user is valid or not.  
> > > Does that make more sense?
> > 
> > You could use some client-side javascript validation for that.
> > 
> > But if you want to do it server side, you would do something like…
> > 
> > # Assuming you params[:email] and params[:password]
> > 
> > u = User.new(params)  
> > u.valid?
> > 
> > email\_invalid, password\_invalid =  
> > u.errors.on(:email), u.errors.on(:password)
> > 
> > unless email\_invalid or password\_invalid  
> > user = User.find(:first, :conditions =\> [‘email = ?’, params[:email]])  
> > blah\_blah\_blah  
> > end
> 
> Dave,
> 
> I think this should do the trick. Thank you for your help.
> 
> - J

Seem to be having a problem…I added the code you suggested in my login  
method and am getting the following error:

NoMethodError in AuthenticationController#login

undefined method `commit=’ for #Member:0x48071b4

The commit field is being generated from the \<%= submit\_tag “Login” %\>  
tag. I’m pretty sure I don’t need this in my model, any ideas?

---

<div class="post-metadata">

**Author:** ![raker\_t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@raker\_t](https://www.ruby-forum.com/u/raker_t)\
**Post date:** [September 25, 2007, 5:39am UTC](https://www.ruby-forum.com/t/validation-in-controllers/115342/7 "2007-09-25T05:39:17Z")

</div>

Hmm… I don’t know. (I’m pretty new to this rails thing, also). Are  
you using form\_for or form\_tag?

Can you post the resulting page source, at least for the relevant bits?
