F
Filament14mo ago
Gush

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'));
}
};
}),
Solution:
how about $get($action->getRecordSelect()->getName())?
Jump to solution
6 Replies
3rgo
3rgo14mo ago
You rule function syntax is not correct, it's 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'));
}
}
]),
See https://filamentphp.com/docs/3.x/forms/validation#custom-rules
Gush
GushOP14mo ago
right but that would be if i had that form in the same place
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'));
}
}
]),
]),
that isnt the case
3rgo
3rgo14mo ago
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
Gush
GushOP14mo ago
doesnt help in this situation i need to somehow get the id of the equipment i select, thx for the help eitherway
Solution
3rgo
3rgo14mo ago
how about $get($action->getRecordSelect()->getName())?
Gush
GushOP14mo ago
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'));
}
};
}),

Did you find this page helpful?