I have a table called "Terroir" with some parameters.
Schema::create('terroirs', function (Blueprint $table) {
$table->id();
$table->string('name', 255);
$table->integer('type')->nullable();
$table->foreignId('terroir_id')->nullable()->constrained('terroirs');
$table->timestamps();
$table->unique(['name', 'type'], 'Terroir');
});
Now if i want to delete a record I want to test with the validation rule if there is an entry of the "id" in the field "terrori_id". Its uses as a hierarchie as a foreign key.
I found the Rule "exist" but I need the opposite "notexist" but this seems not be a valid Rule. Even a check on the foreign_key rule ist only possible with "exist" ?
I can make it without using the validator (just make a query on the table and check if there is a record) but I want to use the validator if possible like this (but it doesn't work):
$validated = $this->validate([
'id' => [
RULE::notIn('terroirs', 'terroir_id'),
],
]);
NotIn checks only values in the array.
Thx for any ideas, Stephan
OK i can see that it works (with DD) but the validate Messages is not dislplayed ?
The Validation :
$validated = $this->validate([
'id' => [
Rule::unique('terroirs', 'terroir_id')->where('terroir_id', $this->terroir->id)
],
]);
Gives me the ID if its not used otherwise it is null. But the Validation error message is not shown. The other message (terroir_id.unique) works fine in another procedure.
public function messages()
{
return [
'terroir_id.unique' => 'The Name "' . $this->name . '" for the Hierarchie already exists !',
'id.unique' => 'The Name "' . $this->name . '" is used as a Hierarchie !',
];
}
And the Modal Form does nothing until I close it.
How do i display the validation fail message ?
->solved, I forgot to handle the new error message in the modal form.
You can use the unique rule which checks that the provided value does not exist in the table.
$validated = $this->validate([
'id' => [
Rule::unique('terroirs', 'terroir_id'),
],
]);
Here you can find the related documentation