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
Hello Jason,
2006/12/20, Jason1706 [email protected]:
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 !
François Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/
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
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
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?
Adam B. 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