Hi group, I built an input form that''s spread over multiple pages. I store the already given data (that match my models and my tables) in a cart-like object. That works so far. But since I dont save the data, when the user inputs it, validations don''t work. They may work on my last (submit) page, but that too late. Any way to make validations work even if they don''t pass the model? Thx in advance Jason --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Francois Beausoleil
2006-Dec-20 15:49 UTC
Re: Validation in a model that is not to be saved
Hello Jason, 2006/12/20, Jason1706 <drehstoph@gmx.net>:> Any way to make validations work even if they don't pass the model?If you have real ActiveRecord objects, you can call #valid? http://api.rubyonrails.com/classes/ActiveRecord/Validations.html#M000800 Hope that helps ! -- Franois Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
straightflush-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-20 15:52 UTC
Re: Validation in a model that is not to be saved
I think you need to get the attributes of the object assigned like @object = Model.new(session[:cart]) @object.additional_attribute = value @object.additional_attribute = value if @object.valid? ..... end I *think* something like that will work. Adam On 12/20/06, Jason1706 <drehstoph-hi6Y0CQ0nG0@public.gmane.org> wrote:> > > Hi group, > > I built an input form that''s spread over multiple pages. I store the > already given data (that match my models and my tables) in a cart-like > object. That works so far. But since I dont save the data, when the > user inputs it, validations don''t work. They may work on my last > (submit) page, but that too late. > > Any way to make validations work even if they don''t pass the model? > > Thx in advance > Jason > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi François,
thx for your hint. So far, I was able to add basic validation to my
model like that
def validate
errors.add_on_empty %w{ title description }
end
And in my controller where I temporarily store that object in my
virtual cart I can probe on validity like that :
def create
@product = Product.new(params[:product])
if @product.valid?
if @cart = find_cart
@cart.add_product(@product)
redirect_to(:action => ''display_cart'')
else
render :action => ''new''
end
else
redirect_to(:action => ''new'')
end
end
But that great validation features for forms are still closed for me at
that point. Anything I dont see? This way, I''d to to all that by hand
.... and I''m a lazy typist :)
Cheers Jason
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
this will be your cup of tea: http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I think the question Jason is asking is how does he validate the
individual fields even though the model isn''t fully baked (because of
the multi-page form). Is that correct?
Validating for presence would be difficult in this situation because you
have maybe three items that need to be present on the first page, two on
the second, and two on the last. But the model instance won''t validate
unless they''re all there.
Here''s an idea. What if you store a ''step'' attribute
in the model, and
only validate if you have reached the proper step? So the controller
sets the step attribute to ''2'' after step 2 and then you put
in the
model:
validates_presence_of :foo, :if => Proc.new { |model| !model.step >= 2 }
Validating format would be similar. You can say something like:
validates_length_of :foo, :within => 5..10,
:if => Proc.new { |model| !model.foo.blank? }
That way the validation won''t fire unless the field has data.
Does that help?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Adam Block wrote:> validates_presence_of :foo, :if => Proc.new { |model| !model.step >= 2 }Whoops. No negation needed there. Should read: validates_presence_of :foo, :if => Proc.new { |model| model.step >= 2 } And where I said ''model'' I was being generic. To be consistent with your previous example, ''model'' should be ''product'' I guess: class Product validates_presence_of :foo, :if => Proc.new { |p| p.step >= 2 } validates_length_of :foo, :within => 5..10, :if => Proc.new { |p| !p.foo.blank? } end /afb -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---