October: formBeforeSave($model) doesn't receive data

Created on 26 Jul 2017  路  3Comments  路  Source: octobercms/october

I'm trying to manipulate a controller, and I can do things with data after saving with:
public function formAfterSave($model) { dd($model->attributes); }
image

but I can't do anything before with:
public function formBeforeSave($model) { dd($model); }
image

Because the function didn't receive any data.... There's some bug, or there are some way to do that?

October build: 419

All 3 comments

Hello @dunets
I think this is a correct behavior. You can see FormController code here:
Https://github.com/octobercms/october/blob/master/modules/backend/behaviors/FormController.php#L226-L263

When you try to save a new object, you have receive a new (empty) Model.

If you want to process some manipulation with arguments, before saving model, you can use Model Events.
http://octobercms.com/docs/database/model#events

@dunets @Samorai is correct, this is the correct behaviour. You would use formBeforeSave($model) as a sort of initializer for the form model before it gets saved. If you want to look at the data of the model immediately before it gets saved, then you would need to do something like this:

MyController.php

public function __construct()
{
    MyModel::extend(function($model) {
        $model->bindEvent('model.beforeSave', function () use ($model) {
            // your code here
        });
    });
    parent::__construct();
}

@LukeTowers thank you!

Was this page helpful?
0 / 5 - 0 ratings