DatePicker and unique: Format is ignored.

I have a DatePicker with the following methods:
DatePicker::make("published_at")
->unique(callback: function(Unique $rule, $livewire) {
return $rule
->where("featured_type_id", $livewire->ownerRecord->id);
})
->format("Y-m-d")
->displayFormat("Y-m-d")
->default(now()->format("Y-m-d"))
->required()
DatePicker::make("published_at")
->unique(callback: function(Unique $rule, $livewire) {
return $rule
->where("featured_type_id", $livewire->ownerRecord->id);
})
->format("Y-m-d")
->displayFormat("Y-m-d")
->default(now()->format("Y-m-d"))
->required()
The published_at field is just a date SQL column (without time). In the form, I wanna see and save the date in Y-m-d format, which is working. The problem is, that the unique rule never hits, because the generated query (checked it in telescope) is the following:
select
count(*) as aggregate
from
`recipes`
where
`published_at` = '2023-03-31 11:39:34'
and `featured_type_id` = '1'
select
count(*) as aggregate
from
`recipes`
where
`published_at` = '2023-03-31 11:39:34'
and `featured_type_id` = '1'
5 Replies
bernhard
bernhard16mo ago
Ok, since unique doesn't seems to work, I solved it with custom rule:
->rules([
function ($livewire) {
return function (string $attribute, $value, Closure $fail) use ($livewire) {
$exists = $value && Recipe::query()
->where("featured_type_id", $livewire->ownerRecord->id)
->where("published_at", substr($value, 0, 10))
->exists();

if ($exists) {
$fail("Für dieses Datum ist bereits ein Rezept eingetragen!");
}
};
},
])
->rules([
function ($livewire) {
return function (string $attribute, $value, Closure $fail) use ($livewire) {
$exists = $value && Recipe::query()
->where("featured_type_id", $livewire->ownerRecord->id)
->where("published_at", substr($value, 0, 10))
->exists();

if ($exists) {
$fail("Für dieses Datum ist bereits ein Rezept eingetragen!");
}
};
},
])
ba_mbi_07
ba_mbi_0715mo ago
can we add ignorerecord in custom rule
Dan Harrin
Dan Harrin15mo ago
why? you have complete control over what happens in your custom field we dont need it as a core feature
ba_mbi_07
ba_mbi_0715mo ago
TextInput::make('slug')
->label('Slug')
->rules([
function ($record) {
return function ($attribute, $value, $fail) {
if (WpTerm::where('slug', $value)
->whereHas('taxonomy', function ($query) {
$query->where('taxonomy', 'category');
})->exists()
) {
$fail("The {$value} must be unique in the 'category' taxonomy.");
}
};
]),
TextInput::make('slug')
->label('Slug')
->rules([
function ($record) {
return function ($attribute, $value, $fail) {
if (WpTerm::where('slug', $value)
->whereHas('taxonomy', function ($query) {
$query->where('taxonomy', 'category');
})->exists()
) {
$fail("The {$value} must be unique in the 'category' taxonomy.");
}
};
]),
i used rule this way to check unique but i can not able figure it out how to use ignoreRecord true as it is making problem while updating
Dan Harrin
Dan Harrin15mo ago
function ($attribute, $value, $fail) use ($record) then you can access $record inside the internal function and add it to your query