GithubHelp home page GithubHelp logo

Cast Date Field about nova-page HOT 3 OPEN

packytagliaferro avatar packytagliaferro commented on June 16, 2024 2
Cast Date Field

from nova-page.

Comments (3)

MannikJ avatar MannikJ commented on June 16, 2024

How can we solve this issue?

from nova-page.

GarethSomers avatar GarethSomers commented on June 16, 2024

Hey anyone reading this;

I fixed it by extending the DateTime field and casting it;

use Carbon\Carbon;
use Laravel\Nova\Fields\DateTime as NovaDateTime;

class DateTime extends NovaDateTime
{
    public function __construct($name, $attribute = null, $resolveCallback = null)
    {
        parent::__construct($name, $attribute, function ($value) {
            if ($value && is_string($value)) {
                try {
                    $possible_date = Carbon::createFromFormat('Y-m-d H:i:s', $value);
                    if ($possible_date) {
                        $value = $possible_date;
                    }
                } catch (\Exception $e) {
                    return null;
                }
            }

            return $value;
        });
    }
}

from nova-page.

jolora avatar jolora commented on June 16, 2024

I've tried extending the DateTime field as @GarethSomers did which removes the error but formats the data in a way that means it does not appear at all in the form or on the details page after saving. So instead I just overwrote the constructor method to remove the casting check:

<?php

namespace App\Nova\Fields;

class DateTime extends \Laravel\Nova\Fields\DateTime 
{
    /**
     * Create a new field.
     *
     * @param  string  $name
     * @param  string|null  $attribute
     * @param  mixed|null  $resolveCallback
     * @return void
     */
    public function __construct($name, $attribute = null, callable $resolveCallback = null)
    {
        parent::__construct($name, $attribute, $resolveCallback ?? function ($value, $request) {
            return $value;
        });
    }
}

Not entirely happy with this solution as I assume there's a good reason for the check but it's the best I can do for now. Would be great if someone knows how to resolve the underlying problem, because currently this package simply doesn't support DateTIme fields.

from nova-page.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.