Make default attribute for Model in October CMS

- 1 answer

Ad

I'm making a backend Ajax form action in Controller.

$this->asExtension('FormController')->create_onSave();

In the field.yaml file there are 4 fields, and all their values go perfectly, validation works.
In the database I need to pass a fifth value: theme, that is just the current active theme, so I don't want to show it on the page.
The problem starts when I'm trying to add it in Model with:

public function beforeSave()
{
    $this->attributes['theme'] = Theme::getActiveThemeCode();
}

After adding this method in Model, all 5 values go to the database, but they skip validation, so in the database I can add empty values. What is causing this to go wrong?

Ad

Answer

Ad

I think that you have to assign the value to the property in the current instance instead of try to overwrite the raw values.

public function beforeSave()
{
  $this->theme = Theme::getActiveThemeCode();
}
Ad
source: stackoverflow.com
Ad