Uh oh!
There was an error while loading. Please reload this page.
Handle edits within frontend fields - #7178
Conversation
|
| $missing = str_random(); | ||
| $old = old($field->handle(), $missing); | ||
| $default = $field->value() ?? $field->defaultValue(); | ||
| $value = $old === $missing ? $default : $old; | ||
There was a problem hiding this comment.
Context for future us, or any onlookers:
This weirdness with the random string old fallback is because of how Laravel's handling of old values works.
If a field is submitted with an empty value, it gets converted to null, so old('fieldname', 'fallback') will give you null. If the field wasn't submitted at all, the fallback would be returned.
If you were to intentionally submit a text field with an empty value, there's no way to tell the difference between the old empty value, or a completely missing value. If we did old('field') ?? $default then we'd end up rendering the default value when a user empties out the field - that's not right.
One option would be to exclude that field from the ConvertEmptyStringsToNulls middleware, but that's overkill and we don't have control of that anyway.
This is the alternative - if the old() method gives us the fallback, we can be sure it's actually missing and not just emptied by the user. I'm using a random string just to prevent someone being able to submit the known string.
Uh oh!
There was an error while loading. Please reload this page.
This PR is related to #6400.
Up until now, front-end fields were only used for the "forms" feature where you only ever create items, not edit them. i.e. you only create submissions.
The fields that get rendered would only need to handle default values and old values.
Now with #6400, you'll need to render the fields but also be able to insert existing values.
This PR adds support for existing values.
valueis the smarter variable that'll account for the value itself, the previously submitted/old value, or default values.old/defaultvariables are no longer passed to the views, but stay there for backwards compatibility.