winter icon indicating copy to clipboard operation
winter copied to clipboard

Disabled fields are submitted and saved

Open goldmont opened this issue 11 months ago • 6 comments

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: Image

How can I solve? Thanks.

Steps to replicate

  1. Add a switch field.
  2. Disable it somehow.
  3. Submit.

Workaround

The solution, for now, seems to be to not disable form fields.

goldmont avatar Jan 04 '25 12:01 goldmont

@goldmont what exactly are you expecting to happen?

LukeTowers avatar Feb 26 '25 19:02 LukeTowers

@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 avatar Feb 26 '25 19:02 goldmont

@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?

LukeTowers avatar Feb 26 '25 19:02 LukeTowers

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.

goldmont avatar Feb 26 '25 19:02 goldmont

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?

LukeTowers avatar Feb 26 '25 19:02 LukeTowers

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.

goldmont avatar Feb 26 '25 20:02 goldmont