Comments (15)
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.
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.
Feel free to reopen if you still having this issue.
from nova-fields.
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.
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.
// 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.
@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.
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.
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 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.
I solved it by creating my own nova date field.
from nova-fields.
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.
Fixed in 0.7.1
from nova-fields.
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.
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.
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)
- fieldClasses, inputClasses and wrapperClasses are not being added to the Field HOT 1
- quickCreate with parameters to populate values not working HOT 1
- readOnly attribute not working on Autocomplete field HOT 3
- quickCreate modal breaks if there are any fields forEach is not a function HOT 4
- Composer install failing?
- Row Sorting HOT 1
- JSON field stores empty data if last child field is empty HOT 4
- Help Texts are not visible any more in JSON field HOT 5
- Row field is not hydrated in form view (undefined error thrown in console) HOT 3
- Panel title on JSON field grouping HOT 1
- JSON in JSON field does not work HOT 1
- production files are 640 kb
- Translation in this package
- Datepicker not open
- Support for alwaysShow() on row field type
- Documentation website is down
- Abandoned?
- Project status
- Nova 4 HOT 12
- TypeError: Cannot read properties of undefined (reading '_c') in nova-fields
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nova-fields.