Disabled fields are submitted and saved
Winter CMS Build
1.2
PHP Version
8.1
Database engine
MySQL/MariaDB
Plugins installed
No response
Issue description
Hi there,
I have a switch field named is_activated in the User form that gets disabled once the user is activated. In my User model, I also have a default value for the attribute is_activated:
public $attributes = [
'is_banned' => false,
'is_activated' => false,
'ldap_authenticated' => false,
];
However, for some reason, when I submit the form (context is always update) the disabled switch is submitted too and the value false is saved for the field is_activated. Thus the user is not activated anymore.
Request payload:
How can I solve? Thanks.
Steps to replicate
- Add a switch field.
- Disable it somehow.
- Submit.
Workaround
The solution, for now, seems to be to not disable form fields.
@goldmont what exactly are you expecting to happen?
@goldmont what exactly are you expecting to happen?
Hi,
I expect the default attributes to be overridden when the record exists. Furthermore, I expect the already existing attribute value to be saved again.
For example, is_activated field by default is false. When a user activates his account, the I set this attribute to true and I disable the form field. Winter is not taking into account the database value but the default one defined into the $attributes array.
@goldmont I don't think the attributes array supports being overridden like that to specify a default value. Why aren't you using the default property on the field configuration?
I didn't mean to change the value inside the $attributes but to consider the record value (when there's any) before saving the model.
However, I prefer the $attributes array other than the default property because I use enums too.
Hmm, I see that method of setting default values is the supported way to do it in Laravel: https://stackoverflow.com/a/39912500.
When you have is_activated true and you disable the switch, is that switch visually appearing as toggled on or off?
Hmm, I see that method of setting default values is the supported way to do it in Laravel: https://stackoverflow.com/a/39912500.
When you have is_activated true and you disable the switch, is that switch visually appearing as toggled on or off?
Yes, it's toggled. After saving, it turns off.