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
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
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
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? -:)
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.
The situation don't change.....
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