Should work like Laravel: if is not fillable or starts with underscore the attribute should be ignored.
It tries to save with the non fillable attribute
Remove a field from fillable and add it to the fields.yaml. The framework will try to use it.
The referenced code is in: Backend\Classes\FormField\FormModelSaver.
I don't understand why this lines are like this:
if ($isNested && is_array($value)) {
$this->setModelAttributes($model->{$attribute}, $value);
} else if ($value !== FormField::NO_SAVE_DATA) {
$model->{$attribute} = $value;
}
Instead of this or something like:
if ($isNested && is_array($value)) {
$this->setModelAttributes($model->{$attribute}, $value);
} else if ($value !== FormField::NO_SAVE_DATA) {
$model->fill([$attribute => $value]);
}
This makes the behavior of the fillable attributes work as it should. Confirm to me if this is good and I will make a pull request for that.
OBS: Maybe this is wrong and I don't understand what's the purpose of the method. If that is the case, explain to me.
There are two contexts to consider, the back-end area and the front-end area. For example, you may want an attribute called is_admin fillable on the backend, but not on the frontend.
FormModelSaver behaves just like fillable, except it will only fill attributes that are defined in the fields.yaml config file. This is the backend area's version of "fillable". The model's $fillable property is more suitable for the front-end, either via components or an API.
Right! So, let me explain my problem: I have a form and I put a checkbox on it, with the name _send, and that is not a column on my table it's just an option to send the saved data by email to some emails on other table.
There is a way to make the model ignore this attribute or I should try some other way?
This is a common problem that has a few solutions
1) Create a custom form widget that explicitly declares it has no save data:
/**
* {@inheritDoc}
*/
public function getSaveValue($value)
{
return FormField::NO_SAVE_DATA;
}
2) Use the October\Rain\Database\Traits\Purgeable model trait
/**
* @var array List of attribute names which should not be saved to the database.
*/
protected $purgeable = ['_send'];
3) Override the formBeforeSave method in your controller to prune the data:
public function formBeforeSave() { unset($_POST['_save']); }
These are all solutions available today. I wasn't aware that fields starting with an underscore would be ignored by Eloquent's fillable, this would be a great way to handle it as well!
4) (Coming soon) Prefix your form field name with an underscore (Str::startsWith($name, '_'))
Thanks @daftspunk! I believe this solves my problem... I'm really thankful for your attention!