olivier Hericord
2005-Aug-06 21:56 UTC
associated objects created with the same FORM....how about validations???
hi, 1/ i have a simple DB schema with 2 tables (for the moment) - the contacts table contains names and so on - the phones table contains the number and a contact_id field 2/ i run the scaffold generator both for contacts and phones... class Contact < ActiveRecord::Base has_many :phones validates_presence_of :first_name, :last_name end class Phone < ActiveRecord::Base belongs_to :contact validates_presence_of :number end 3/ in the new contact view i added a textfield with name="contact[phone][][number]" because i want the user to be abble to enter the first associated phone number right inside the contact creation form. in the controller i do get the phone number on form submiting....but How can i launch the phone validation on that number from the contact controller? if the validation fails how can i make the redisplayed form to highlight the phone number field? am i clear? is what i want to achieve possible? thanks olivier
Felix Ingram
2005-Aug-07 11:36 UTC
Re: associated objects created with the same FORM....how about validations???
On 8/6/05, olivier Hericord <olivier.hericord.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: <snip>> 2/ > i run the scaffold generator both for contacts and phones... > > class Contact < ActiveRecord::Base > has_many :phones > validates_presence_of :first_name, :last_name > end > > class Phone < ActiveRecord::Base > belongs_to :contact > validates_presence_of :number > end> in the controller i do get the phone number on form submiting....but > How can i launch the phone validation on that number from the contact > controller?Have you tried: validates_associated? Add ''validates_associated :phones'' to your Contact model. According to the api docs this will ensure that ''phones'' is valid. I haven''t tested this though.> if the validation fails how can i make the redisplayed form to > highlight the phone number field?No idea. Anyone? Hope that helps, F.
olivier Hericord
2005-Aug-07 13:14 UTC
Re: associated objects created with the same FORM....how about validations???
yes i tried validates_associated but i just want to know how to redirect the generated error in case of phone validation fails to the contact controller and then to the field that raises the error On 8/7/05, Felix Ingram <f.ingram.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/6/05, olivier Hericord <olivier.hericord.lists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > <snip> > > 2/ > > i run the scaffold generator both for contacts and phones... > > > > class Contact < ActiveRecord::Base > > has_many :phones > > validates_presence_of :first_name, :last_name > > end > > > > class Phone < ActiveRecord::Base > > belongs_to :contact > > validates_presence_of :number > > end > > > in the controller i do get the phone number on form submiting....but > > How can i launch the phone validation on that number from the contact > > controller? > > Have you tried: validates_associated? > Add ''validates_associated :phones'' to your Contact model. According > to the api docs this will ensure that ''phones'' is valid. I haven''t > tested this though. > > > if the validation fails how can i make the redisplayed form to > > highlight the phone number field? > > No idea. Anyone? > > Hope that helps, > > F. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >