Validation in a model that is not to be saved

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,

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 :slight_smile:

Cheers Jason

this will be your cup of tea:

http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects