Using Related Field Data in Validation Messages

Can anyone tell me how to pass related data into the Validation message? Currently, this works as I want it to; I just need a better error message.
Select::make('entity_type_id')
->label('Entity Type')
->relationship('entityType', 'name')
->required(),
TextInput::make('name')
->required()
->unique('entities', 'name')
->validationMessages([
'unique' => 'This {{I want to display entityType.name here}} already has an attribute with this name.',
])
->maxLength(255),
Select::make('entity_type_id')
->label('Entity Type')
->relationship('entityType', 'name')
->required(),
TextInput::make('name')
->required()
->unique('entities', 'name')
->validationMessages([
'unique' => 'This {{I want to display entityType.name here}} already has an attribute with this name.',
])
->maxLength(255),
3 Replies
EleventhTower
EleventhTowerOP8mo ago
I managed to get what I want with this:
Select::make('entity_type_id')
->label('Entity Type')
->relationship('entityType', 'name')
->required(),
TextInput::make('name')
->required()
->unique('entities', 'name')
->validationMessages([
'unique' => function ($get) {
$entityTypeName = EntityType::where('id', $get('entity_type_id'))->first()->name;
return "This {$entityTypeName} already has an entity with this name.";
},
])
->maxLength(255),
Select::make('entity_type_id')
->label('Entity Type')
->relationship('entityType', 'name')
->required(),
TextInput::make('name')
->required()
->unique('entities', 'name')
->validationMessages([
'unique' => function ($get) {
$entityTypeName = EntityType::where('id', $get('entity_type_id'))->first()->name;
return "This {$entityTypeName} already has an entity with this name.";
},
])
->maxLength(255),
Is there a better way to do this?
brahim
brahim8mo ago
CH
EleventhTower
EleventhTowerOP8mo ago
Just an update that I found with other tests while implementing this, I had to change the implemenation of the unique rule to use modifyRuleUsing.
Select::make('entity_type_id')
->label('Entity Type')
->relationship('entityType', 'name')
->required(),
TextInput::make('name')
->required()
->unique(modifyRuleUsing: fn(Unique $rule, $get) => $rule->where('entity_type_id', get('entity_type_id')))
->validationMessages([
'unique' => function ($get) {
$entityTypeName = EntityType::where('id', $get('entity_type_id'))->first()?->name;
return "The Entity Type '{$entityTypeName}' already has an entity with this name.";
},
])
->maxLength(255),
Select::make('entity_type_id')
->label('Entity Type')
->relationship('entityType', 'name')
->required(),
TextInput::make('name')
->required()
->unique(modifyRuleUsing: fn(Unique $rule, $get) => $rule->where('entity_type_id', get('entity_type_id')))
->validationMessages([
'unique' => function ($get) {
$entityTypeName = EntityType::where('id', $get('entity_type_id'))->first()?->name;
return "The Entity Type '{$entityTypeName}' already has an entity with this name.";
},
])
->maxLength(255),
Want results from more Discord servers?
Add your server