Validation of other model fields

I've got in my Form not only fileds from my model but also fields from ither models. How can I validate them like other fields.

Example names od input tag fields

<p><label for="model1_name1">Name1</label> <%= text_field 'model1', 'name1' , :size => 40 %></p>

<p><label for="model1_name2">Name2</label> <%= text_field 'model1', 'name2' , :size => 40 %></p>

and from different model:

<p><label for="model2_name3">Name3</label> <%= text_field 'model2', 'name3' , :size => 40 %></p>

How can I validate presence_of field name3 from model2 model??

The update_attributes method saves as well as updating the attribute.

b

Stephan Wehner wrote:

Daniello, hopefully the following will help move you forward. It will leave you with an @errors for both models in your view that you can iterate over

When managing validations for multiple models in one action I typically use something like the following:

     @model1 = Model1.new(params[:model1])      @model2 = Model2.new(params[:model2])

     respond_to do |format|        if @ model1.valid? && @ model2.valid?          #TODO: transaction here would help out          @ model1.save(false)          flash[:notice] = 'Model1 and Model2 were successfully created.'          format.html { render :partial => "my_partial" }        else          flash[:notice] = 'There were errors validating model1, model2'          #merge_errors (below), merges the errors of 2 models into one errors list          format.html { @errors = merge_errors(@model1, @model2); render :partial => "/error_messages", :errors => @errors, :status => 500 }        end      end

   #merges errors from 2 instances into 1 error object    def merge_errors(model1, model2)      if model1.errors.size > 0 && model2.errors.size > 0        model1.errors.instance_eval { @errors.merge model2.errors.instance_values['errors'] }      elsif model1.errors.size > 0        model1.errors      elsif model2.errors.size > 0        model2.errors      else        model1.errors      end    end

Cheers, Jodi General Partner The nNovation Group inc. www.nnovation.ca/blog

on-innovation.gif

Daniello,

If there is a model association between the two models you can use the following (copy/pasted from api.rubyonrails.org):

validates_associated(*attr_names)

Validates whether the associated object or objects are all valid themselves. Works with any kind of association.

  class Book < ActiveRecord::Base     has_many :pages     belongs_to :library

    validates_associated :pages, :library   end

Warning: If, after the above definition, you then wrote:

  class Page < ActiveRecord::Base     belongs_to :book

    validates_associated :book   end

...this would specify a circular dependency and cause infinite recursion.

NOTE: This validation will not fail if the association hasn't been assigned. If you want to ensure that the association is both present and guaranteed to be valid, you also need to use validates_presence_of.

Configuration options:

    * on Specifies when this validation is active (default is :save, other options :create, :update)     * if - Specifies a method, proc or string to call to determine if the validation should

occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.

Regards, Bas van Westing

now how did I miss that ?!

This is clearly a better route to go - for you and me, Daniello.

thanx Bas

Jodi