-
-
Notifications
You must be signed in to change notification settings - Fork 546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
value
is the smarter variable that'll account for the value itself, the previously submitted/old value, or default values.old
/default
variables are no longer passed to the views, but stay there for backwards compatibility.