Topic: validates_presence_of doesn't work

Can validates_presence_of be used on any field and on any form? It doesn't work. It doesn't send the error message or goes back to the form.

Re: validates_presence_of doesn't work

Can you post the code in your model and any snippets from your logfile that seem relevant?

Re: validates_presence_of doesn't work

Yes. Here it is. First the form, in a controller called "intro" and action "publicsurvey".

<h2>About you</h2>

<%= start_form_tag :controller => 'publicuser', :action => 'login'%>
<p><label>Email (*)</label>
<%= text_field 'publicuser', 'mail' %></p>
<p><label>Country</label>
<%= country_select 'publicuser', 'country' %></p>
<p><label>Gender</label>
<%= select 'publicuser', 'sex', ['','Male','Female'], {}, :style => 'width:80px'%></p>
<p><label>Age</label>
<%= select 'publicuser', 'age', ['','-15','15-20','20-30','30-40','40-50','50-60','60-70','70-'], {}, :style => 'width:80px' %></p>
<p><label>Profession</label>
<%= select 'publicuser', 'profession', ['','Creative/Designer','Worker','Manager', 'Administrator','Marketing','Leadership','Engineer','Scientist', '.....']  %></p>
<%= hidden_field 'publicuser', 'ip', :value => request.remote_ip %>
<div class="submit">
  <%= submit_tag "Next" %>
  </div>
<%= end_form_tag%>


Then the model.

class Publicuser < ActiveRecord::Base

  validates_presence_of :mail, :message => "Email can not be blank"

end


The action in publicuser_controller. The action is kind of ruff. I'm still working on that.

before_filter :authorizepublicuser, :except => :login
 
  def login
    @publicuser = Publicuser.new(params[:publicuser])
    flash[:notice] = @publicuser
    if request.post?
      @publicuserexist = Publicuser.find(:all, :conditions => ["mail=?",@publicuser.mail])
      if @publicuserexist.size == 0
        @publicuser.save
        flash[:notice] = "You succesfully created a user. Now you can start the survey."     
        redirect_to :controller => 'intro', :action => 'publicsurvey' 
      else
        flash[:notice] = "Welcome back"     
        redirect_to :controller => 'intro', :action => 'publicsurvey'
      end
    end
  end

This is what I have in application controller. For now I've commented it.

  def authorizepublicuser
#    unless Publicuser.find_by_id(session[:user_id])
#      flash[:notice] = "Please log in"
#      redirect_to(:controller => "intro", :action => "publicsurvey")
#    end
  end

Re: validates_presence_of doesn't work

Do the form and action have to be in the same controller in order for a validates_presence_of to work?

Re: validates_presence_of doesn't work

I've moved it over to the same controller (but not the "same" as the model). Still doesn't work.

Re: validates_presence_of doesn't work

Works when I use @publicuser.save in the condition. Why?

if request.post? and @publicuser.save

Re: validates_presence_of doesn't work

But any :message doesn't show, just a red error color surrounding the field.

Re: validates_presence_of doesn't work

You need to add <%= error_messages_for :publicuser %> to your view

Your code is a little strange too. try something like this:

   def login
     @publicuser = Publicuser.new(params[:publicuser])
     flash[:notice] = @publicuser
     if request.post?
       @publicuserexist = Publicuser.find(:all, :conditions => ["mail=?",@publicuser.mail])
       if @publicuserexist.size == 0
         if @publicuser.save
           flash[:notice] = "You succesfully created a user. Now you can start the survey."     
           redirect_to :controller => 'intro', :action => 'publicsurvey' 
        else # failed to save
          render :action => 'login' and return
        end
     else
       flash[:notice] = "Welcome back"     
       redirect_to :controller => 'intro', :action => 'publicsurvey'
     end
   end
end

ProgrammingBooks.org - Programming Books Ranked by Programmers
Turn of the Crank - My Personal Blog
Robot Walrus - My art prints & posters blog
My Game Ideas - Submit, rate, view video game Ideas

Re: validates_presence_of doesn't work

ryanb wrote:

Are you outputting the error messages in the view? For example:

<%= error_messages_for :my_model %>

No. But that's not needed in another login setup I have - there's just the flash[:notice]. How come it works there?

Re: validates_presence_of doesn't work

Sorry. Wrong by me. The <%= error_messages_for :my_model %> is where it should be, but not in the setup that doesn't work. Now it works.