Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

onBeforeWrite: copy value from a field to another field...


Go to End


5 Posts   4793 Views

Avatar
biapar

Forum Moderator, 435 Posts

29 June 2010 at 8:53pm

Edited: 03/07/2010 1:26am

Hi,

is it possible, before save a page, to copy data from one field to another field?
That is I've a field A and a field B. After compiled A and leave field A, value of A must be filled in field B.

Thank you

Avatar
biapar

Forum Moderator, 435 Posts

3 July 2010 at 1:26am

Edited: 03/07/2010 1:26am

Into product.php controller, I write:

 protected function onBeforeWrite() {

        parent::onBeforeWrite();


        if ($this->ID) {

            $this->record['QuotaDisponibile'] = $this->record['PriceProduct'];
// fill in MetaDescription without any tags

          if ($this->record['Content']) {

                $this->record['MetaDescription'] = strip_tags($this->record['Content']);
            }

            if ($this->record['Content']) {

                $this->record['MetaKeywords'] = self::calculateKeywords($this->record['Content'], 4, 15);
            }
        }

        $this->record['QuotaDisponibile'] = $this->record['PriceProduct'];
    }

but don't work?
How can view debug? Is there a debug type Visual Studio? -:)

Avatar
MarcusDalgren

Community Member, 288 Posts

3 July 2010 at 4:25am

If I remember correctly $this->record refers to the database record and since this is onBeforeWrite it hasn't been written to the database yet. Instead of writing $this->record['QuotaDisponibile'] do $this->QuotaDisponibile instead. The same goes for all your other $this->record calls.

Avatar
biapar

Forum Moderator, 435 Posts

3 July 2010 at 7:42pm

Edited: 08/07/2010 1:07am

The situation don't change.....

Avatar
Simkim

Community Member, 9 Posts

14 July 2010 at 8:02am

Hi,

I have made something similar. In your function onBeforeWrite, you have to retrieve the value of the field, and after that you can copy it to the new field. Here you have the functions I have used:

function onBeforeWrite(){
$firstField = $this->getField('yourFieldName');
$this->setField('yourNewFieldName', $firstField);

/*You can also use the setCastedField function*/

parent::onBeforeWrite();
}

Hope this will help you!

Regards,

Mercedes