8000 Pre-process date values by Healyhatman · Pull Request #706 · LaravelCollective/html · GitHub
[go: up one dir, main page]

Skip to content

Pre-process date values #706

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

Open
wants to merge 1 commit into
base: 6.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Pre-process date values
Given that a form is opened with Form::model
When using Form::date to create a date field and leaving the value null to fetch the value from the model, when the value is a Carbon then the value is set as Y-m-d H:i:s and the appropriate date is not selected. If you pass a carbon directly to the value (instead of null) then it is correctly formatted to Y-m-d.

This pull request pre-processes the value when it's null using getValueAttribute. Additionally, it replaces DateTime with DateTimeInterface to allow the use of CarbonImmutable
  • Loading branch information
Healyhatman authored Feb 15, 2022
commit 6393cd36f2e0de2ce45dc5da95e07c1243ab1daf
4 changes: 3 additions & 1 deletion src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,9 @@ public function number($name, $value = null, $options = [])
*/
public function date($name, $value = null, $options = [])
{
if ($value instanceof DateTime) {
$value ??= $this->getValueAttribute($name, $value);

if ($value instanceof DateTimeInterface) {
$value = $value->format('Y-m-d');
}

Expand Down
0