Page 1 of 1

How do I populated a field with another field automatically?

PostPosted: Fri Jul 15, 2011 10:25 am
by rlevin
My problem seems relatively simple, if fieldB == "", then fieldB == fieldA.

Correct me if im wrong but in my case I would use beforesave(). When I click save I want to check if the textbox value for fieldB is empty. If so, set the textbox value of fieldA to the fieldB field. How do I reference the textbox value of those fields?

Re: How do I populated a field with another field automatically?

PostPosted: Fri Jul 15, 2011 10:33 am
by shannah
Yes. the beforeSave() hook sounds like the right place for this:
Code: Select all
function beforeSave($record){
   if ( !$record->val('fieldA') ){
      $record->setValue('fieldA', $record->val('fieldB'));
   }
}

Re: How do I populated a field with another field automatically?

PostPosted: Fri Jul 15, 2011 10:41 am
by rlevin
I dont know why but I was assuming record->val('fieldA') looked at the value within the database not the textbox itself.

Re: How do I populated a field with another field automatically?

PostPosted: Fri Jul 15, 2011 10:47 am
by shannah
The $record parameter passed to beforeSave is a Dataface_Record object. Its values at that time include any unsaved changes (i.e. any data entered from an edit form).

Re: How do I populated a field with another field automatically?

PostPosted: Fri Jul 15, 2011 10:48 am
by rlevin
Thank you!