Coder Social home page Coder Social logo

Comments (15)

Abdukhaligov avatar Abdukhaligov commented on August 20, 2024 1
    JSON::make('Meta', [
        Boolean::make('Winner')
            ->yesLabel('Yea!')
            ->noLabel('Nope'),
        Date::make('Win Date', 'win_date')
            ->resolveUsing(function ($date){
              return Carbon::parse($date);
            })
            ->format('DD MMM YYYY'),
        Number::make('Points')
            ->min(0)
            ->max(100)
    ]),

from nova-fields.

beliolfa avatar beliolfa commented on August 20, 2024

Hey! sorry for the delay. I was in a christmas vacation and I had no time at all.

Can you try this and tell me how it goes?

JSON::make('Meta', [
       Boolean::make('Winner')
              ->yesLabel('Yea!')
              ->noLabel('Nope'),
       Date::make('Win Date', 'win_date')
             ->format('DD MMM YYYY'),
       Number::make('Points')
             ->min(0)
             ->max(100)
       ]),->fillUsing(function (NovaRequest $request, $model, $attribute, $requestAttribute) {
            $value = $request[$requestAttribute];
             // cast your date to a Carbon instance or whatever you need
             dd($value);
             $newValue = "whatever you did";
            // reasign to your model
            $model->{$attribute} = $newValue
        }),

Thanks!

from nova-fields.

beliolfa avatar beliolfa commented on August 20, 2024

Feel free to reopen if you still having this issue.

from nova-fields.

CBreetzi avatar CBreetzi commented on August 20, 2024

Thank you very much for this package - it's great!

I'm still having troubles with the date field in JSON columns and the solution you provided above didn't work out.

protected function dataFields()
{
        return [
            JSON::make('Data', [
                Date::make('Birthday',"birthday")
                ->format('DD MMM YYYY')
                ->hideFromIndex()
            ])
        ];
}

How do I insert the "fillUsing" method here? Are there any other ways of solving this without creating a dedicated column in the database table?

Thanks!

from nova-fields.

tanthammar avatar tanthammar commented on August 20, 2024

I have the same problem.
I tried to cast the nested date value on the model to silence Nova errors.
But it did not help.

To make it more clear it is not saving as date that is the problem it is Nova that throws the error because it doesn't find the field name in the $casts array on the model.

This throws an error :
date in json

// in model:
protected $casts = [
    'rrule' => 'array',
     'dtstart' => 'datetime:Y-m-d',
     'until' => 'datetime:Y-m-d'
];

//in Nova resource:
Json::make('Rrule', [
   Date::make('Start', 'dtstart'),
   Date::make('End', 'until'),
])

Moving the date fields outside of the Json::make silences the error but then you cannot save unless we create extra fields in DB.

// in model:
protected $casts = [
    'rrule' => 'array',
     'dtstart' => 'datetime:Y-m-d',
     'until' => 'datetime:Y-m-d'
];

//in Nova resource 
Date::make('Start', 'dtstart'),
Date::make('End', 'until'),
Json::make('Rrule', [
   //other fields
])

I also tried this, with no luck


protected $casts = [
        'rrule' => 'array',
        'dtstart' => 'datetime:Y-m-d',
        'until' => 'datetime:Y-m-d'
];
protected $appends = [
            'dtstart',
            'until'
];

function getDtstartAttribute() {
            return array_get($this->rule, 'dtstart', null);
}

function getUntilAttribute() {
            return array_get($this->rule, 'until', null);
}

I suppose this is what returns the error:

class Date extends Field
{
public function __construct($name, $attribute = null, $resolveCallback = null)
    {
        parent::__construct($name, $attribute, $resolveCallback ?? function ($value) {
            if (! $value instanceof DateTimeInterface) {
                throw new Exception("Date field must cast to 'date' in Eloquent model.");
            }

            return $value->format('Y-m-d');
        });
    }

Is there a way to get around it?

from nova-fields.

beliolfa avatar beliolfa commented on August 20, 2024

@CBreetzi please can you try this fillUsing and tell me how it goes?

JSON::make('Data', [
                Date::make('Birthday', "birthday")
                ->format('DD MMM YYYY')
                ->hideFromIndex()
            ])
            ->fillUsing(function (NovaRequest $request, $model, $attribute, $requestAttribute) {
                $value = $request[$requestAttribute];
                $value['birthday'] = new \DateTime($value['birthday']);
            }),

@tanthammar is this solution also valid for your case?

from nova-fields.

yabasha avatar yabasha commented on August 20, 2024

I did like what you said but I'm still getting Date field must cast to 'date' in Eloquent model exception.

// In model
protected $casts = [
    'meta' => 'object',
];

Any other ideas,

Hey! sorry for the delay. I was in a christmas vacation and I had no time at all.

Can you try this and tell me how it goes?

JSON::make('Meta', [
       Boolean::make('Winner')
              ->yesLabel('Yea!')
              ->noLabel('Nope'),
       Date::make('Win Date', 'win_date')
             ->format('DD MMM YYYY'),
       Number::make('Points')
             ->min(0)
             ->max(100)
       ]),->fillUsing(function (NovaRequest $request, $model, $attribute, $requestAttribute) {
            $value = $request[$requestAttribute];
             // cast your date to a Carbon instance or whatever you need
             dd($value);
             $newValue = "whatever you did";
            // reasign to your model
            $model->{$attribute} = $newValue
        }),

Thanks!

from nova-fields.

tanthammar avatar tanthammar commented on August 20, 2024

Thank you for your support on this!
Tried your suggestion but it did not work, still getting "..must cast to date error".
But, there is a new error in the log file from the fillUsing(function)

local.ERROR: Argument 1 passed to Laravel\Nova\Resource::resolveFields() must be an instance of Laravel\Nova\Http\Requests\NovaRequest, instance of Illuminate\Http\Request given,

This is what I tried:

protected function rrule()
    {
        return [
                 Date::make('Start', 'dtstart')
                ->withMeta(['value' => $this->rrule['dtstart'] ?? now()->format('Y-m-d h:m:s')])
                ->rules('required')
                ->help('Required. The recurrence start.')
                ->fillUsing(function (NovaRequest $request, $model, $attribute, $requestAttribute) {
                    $value = $request[$requestAttribute];
                    $value['dtstart'] = new \DateTime($value['dtstart']);
                }),
                Date::make('End', 'until')
                ->help('Optional. The limit of the recurrence. Leave empty for unlimited number of events.')
                ->fillUsing(function (NovaRequest $request, $model, $attribute, $requestAttribute) {
                    $value = $request[$requestAttribute];
                    $value['until'] = new \DateTime($value['until']);
                }),
         ];
}

from nova-fields.

CBreetzi avatar CBreetzi commented on August 20, 2024

@CBreetzi please can you try this fillUsing and tell me how it goes?

JSON::make('Data', [
                Date::make('Birthday', "birthday")
                ->format('DD MMM YYYY')
                ->hideFromIndex()
            ])
            ->fillUsing(function (NovaRequest $request, $model, $attribute, $requestAttribute) {
                $value = $request[$requestAttribute];
                $value['birthday'] = new \DateTime($value['birthday']);
            }),

@tanthammar is this solution also valid for your case?

Thank you for assistance @disitec - sorry for the late reply.
I've tried your solution but it didn't work. I'm getting the same error as @tanthammar:

local.ERROR: Argument 1 passed to Laravel\Nova\Resource::resolveFields() must be an instance of Laravel\Nova\Http\Requests\NovaRequest, instance of Illuminate\Http\Request given,

from nova-fields.

tanthammar avatar tanthammar commented on August 20, 2024

I solved it by creating my own nova date field.

from nova-fields.

beliolfa avatar beliolfa commented on August 20, 2024

Thats great @tanthammar !
There is no R64/Date field at the moment. Would you consider doing a PR for that so anyone having this issue could use your field? That would be awesome.

Thanks!

from nova-fields.

beliolfa avatar beliolfa commented on August 20, 2024

Fixed in 0.7.1

from nova-fields.

nickpoulos avatar nickpoulos commented on August 20, 2024

I am still seeing this behavior in v0.7.3, however this time using the DateTime field instead of just Date. I will attempt a fix using same solution as the Date fix.

Just submitted a PR for fix.

from nova-fields.

acidjazz avatar acidjazz commented on August 20, 2024

I stumbled upon this with actual nova dates and this worked for me:

                Date::make('Timer end date', 'content->timer_end_date')
                    ->rules('required')
                    ->default(fn() => '2021-12-08')
                    ->resolveUsing(function ($date) {
                        return Carbon::parse($date)->format('Y-m-d');
                    }),

from nova-fields.

martin55 avatar martin55 commented on August 20, 2024

Hi! I just ran into the similar issue. This is how I solved it using the laravel accessor:

public function getContentAttribute(string $value): array
  {
      $value = (array)json_decode($value);
      if (isset($value['timer_end_date']) && $value['timer_end_date']) {
          $value['timer_end_date'] = Carbon::parse($value['timer_end_date']);
      }
      return $value;
  }

from nova-fields.

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.