Custom Validation with relation

->rules([function ($record) {
return function (string $attribute, $value, Closure $fail) use ($record) {
if (Model::where('agreement_id', $replaceWithRelationID)->whereDate('start_date', '>=', $value)->exists()) {
$fail("There is already a price agreement right/after this date");
}
};
}]),
->rules([function ($record) {
return function (string $attribute, $value, Closure $fail) use ($record) {
if (Model::where('agreement_id', $replaceWithRelationID)->whereDate('start_date', '>=', $value)->exists()) {
$fail("There is already a price agreement right/after this date");
}
};
}]),
I need to replace replaceWithRelationID with the parameter ID from the url or the relationship its on.
8 Replies
Husky110
Husky11013mo ago
if your slug is like mypage/{relationID} and you have a public $relationIDin your Page, you could access it like $this->relationID.
Husky110
Husky11013mo ago
or you use it in a querystring -> info on that can be found here: https://laravel-livewire.com/docs/2.x/rendering-components
Livewire
Rendering Components | Livewire
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
BlackShadow
BlackShadow13mo ago
Its a the normal Filament Formbuilder tho /admin/company-agreements/9/edit This has a PriceRelationManager where i need to get access to the relation id 9
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\DatePicker::make('start_date')
->rules([function ($record) {
return function (string $attribute, $value, Closure $fail) use ($record) {
$id = 9; // Replace with relation id
if (CompanyAgreementPrice::where('agreement_id', $id)->whereDate('start_date', '>=', Carbon::parse($value))->exists()) {
$fail("There is already a price agreement on/after this date");
}
};
}])
->required(),

Forms\Components\DatePicker::make('end_date')
->afterOrEqual('start_date'),

TextInput::make('price')
->required()
->mask(fn(TextInput\Mask $mask) => $mask->money(prefix: '€'))

])->columns(3);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\DatePicker::make('start_date')
->rules([function ($record) {
return function (string $attribute, $value, Closure $fail) use ($record) {
$id = 9; // Replace with relation id
if (CompanyAgreementPrice::where('agreement_id', $id)->whereDate('start_date', '>=', Carbon::parse($value))->exists()) {
$fail("There is already a price agreement on/after this date");
}
};
}])
->required(),

Forms\Components\DatePicker::make('end_date')
->afterOrEqual('start_date'),

TextInput::make('price')
->required()
->mask(fn(TextInput\Mask $mask) => $mask->money(prefix: '€'))

])->columns(3);
}
Husky110
Husky11013mo ago
Oh - okay. So just to get you right: url looks like this /admin/company-agreements/{WHATEVER}/edit and you wanna access the "WHATEVER"?
BlackShadow
BlackShadow13mo ago
yea basically, unless there is another way Maybe you can access the relation where the price will be saved on or something
Husky110
Husky11013mo ago
Have you tried it via self::getRecord()?
BlackShadow
BlackShadow13mo ago
Oh wow, thats it! Thanks ❤️