get select record on relation manager attach form
Hey, i need help understanding how do i get the equipment id i selected on form in order to make a custom rule checking for existing records with the same equipment_id and serial_number
here is the code
->rule(static function (Forms\Get $get, Forms\Components\Component $component): Closure {
return static function (string $attribute, $value, Closure $fail) use ($get, $component) {
$equipmentID = ;
$existingRecord = DB::table('construction_has_equipment')
->where('serial_number', $value)
->where('equipment_id', $equipmentID)
->exists();
if ($existingRecord) {
$fail(__('construction-equipment.construction_equipment_resource.error'));
}
};
}),
->rule(static function (Forms\Get $get, Forms\Components\Component $component): Closure {
return static function (string $attribute, $value, Closure $fail) use ($get, $component) {
$equipmentID = ;
$existingRecord = DB::table('construction_has_equipment')
->where('serial_number', $value)
->where('equipment_id', $equipmentID)
->exists();
if ($existingRecord) {
$fail(__('construction-equipment.construction_equipment_resource.error'));
}
};
}),
6 Replies
You rule function syntax is not correct, it's
See https://filamentphp.com/docs/3.x/forms/validation#custom-rules
rules
and the function inside should not be static. Then you can use $get
:
->rules([fn (Forms\Get $get, Forms\Components\Component $component): Closure =>
function (string $attribute, $value, Closure $fail) use ($get, $component) {
$equipmentID = $get('equipment_id');
$existingRecord = DB::table('construction_has_equipment')
->where('serial_number', $value)
->where('equipment_id', $equipmentID)
->exists();
if ($existingRecord) {
$fail(__('construction-equipment.construction_equipment_resource.error'));
}
}
]),
->rules([fn (Forms\Get $get, Forms\Components\Component $component): Closure =>
function (string $attribute, $value, Closure $fail) use ($get, $component) {
$equipmentID = $get('equipment_id');
$existingRecord = DB::table('construction_has_equipment')
->where('serial_number', $value)
->where('equipment_id', $equipmentID)
->exists();
if ($existingRecord) {
$fail(__('construction-equipment.construction_equipment_resource.error'));
}
}
]),
right but that would be if i had that form in the same place
that isnt the case
Tables\Actions\AttachAction::make()
->recordSelect(
fn (Select $select) => $select->noSearchResultsMessage(__('equipment.equipment_resource.search_field')),
)
->color('success')
->preloadRecordSelect()
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('serial_number')
->numeric()
->minValue(1)
->label(__('construction-equipment.construction_equipment_resource.serial_number'))
// ->unique(ignoreRecord: true)
->required()
->maxLength(255)
->rules([
fn (Forms\Get $get, Forms\Components\Component $component): Closure =>
function (string $attribute, $value, Closure $fail) use ($get, $component) {
$equipmentID = $get('equipment_id');
dd($equipmentID);
$existingRecord = DB::table('construction_has_equipment')
->where('serial_number', $value)
->where('equipment_id', $equipmentID)
->exists();
if ($existingRecord) {
$fail(__('construction-equipment.construction_equipment_resource.error'));
}
}
]),
]),
Tables\Actions\AttachAction::make()
->recordSelect(
fn (Select $select) => $select->noSearchResultsMessage(__('equipment.equipment_resource.search_field')),
)
->color('success')
->preloadRecordSelect()
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('serial_number')
->numeric()
->minValue(1)
->label(__('construction-equipment.construction_equipment_resource.serial_number'))
// ->unique(ignoreRecord: true)
->required()
->maxLength(255)
->rules([
fn (Forms\Get $get, Forms\Components\Component $component): Closure =>
function (string $attribute, $value, Closure $fail) use ($get, $component) {
$equipmentID = $get('equipment_id');
dd($equipmentID);
$existingRecord = DB::table('construction_has_equipment')
->where('serial_number', $value)
->where('equipment_id', $equipmentID)
->exists();
if ($existingRecord) {
$fail(__('construction-equipment.construction_equipment_resource.error'));
}
}
]),
]),
Oh this is the AttachAction from a relation manager ? This is different then.
I've never done it, but apparently, you can pass data to the relation manager : https://filamentphp.com/docs/3.x/panels/resources/relation-managers#passing-properties-to-relation-managers
doesnt help in this situation i need to somehow get the id of the equipment i select, thx for the help eitherway
Solution
how about
$get($action->getRecordSelect()->getName())
?that did the trick, thank you!
leaving my code here if anyone in the future needs
->rule(static function (Forms\Get $get, Forms\Components\Component $component) use ($action): Closure {
return static function (string $attribute, $value, Closure $fail) use ($get, $component, $action) {
$equipmentID = $get($action->getRecordSelect()->getName());
$existingRecord = DB::table('construction_has_equipment')
->where('serial_number', $value)
->where('equipment_id', $equipmentID)
->exists();
if ($existingRecord) {
$fail(__('construction-equipment.construction_equipment_resource.error'));
}
};
}),
->rule(static function (Forms\Get $get, Forms\Components\Component $component) use ($action): Closure {
return static function (string $attribute, $value, Closure $fail) use ($get, $component, $action) {
$equipmentID = $get($action->getRecordSelect()->getName());
$existingRecord = DB::table('construction_has_equipment')
->where('serial_number', $value)
->where('equipment_id', $equipmentID)
->exists();
if ($existingRecord) {
$fail(__('construction-equipment.construction_equipment_resource.error'));
}
};
}),