Custom Validation Message for Required Pivot Select Field in Backpack Relationship Field #1273
-
I have created a relationship field in my ProductCrudController:
I need to add a custom validation message for the required pivot select field (the title attribute in the pivot relation) in my ProductRequest. Here’s what I want to achieve:
My current challenge: I’m unsure what the label should look like in the rules() and messages() methods to target the pivot field validation specifically. Could anyone guide me on how to correctly define the validation rules and messages for this case? Thanks!! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
To add custom validation for the required pivot select field in your Backpack relationship field, you need to modify your ProductRequest class. Based on the information from the knowledge sources, here's how you can achieve this:
public function rules()
{
return [
'articles.*.articles' => 'required',
// other rules...
];
}
public function messages()
{
return [
'articles.*.articles.required' => 'Please select an article or remove the empty block.',
// other messages...
];
} The key It's important to note that the field name in the validation rules and messages should match the name you've given to the relationship field in your CRUD controller, which in this case is 'articles'. The For more information on validation in Backpack, you can refer to the Backpack CRUD documentation. If you need to further customize the validation messages or behavior, you might want to look into overriding the Remember that validation for repeatable fields and pivot fields can sometimes be tricky. If you encounter any issues, double-check that your field names in the validation rules exactly match those in your CRUD field definition. ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: Github Issue (closed): [Bug] Unexpected "The pivot field is required" form validation error message for relationship field |
Beta Was this translation helpful? Give feedback.
-
Hello @ceeckhout I'm glad our Bot was able to answer your question. I will close this issue, but please feel free to re-open or create a new one if needed. Cheers. |
Beta Was this translation helpful? Give feedback.
To add custom validation for the required pivot select field in your Backpack relationship field, you need to modify your ProductRequest class. Based on the information from the knowledge sources, here's how you can achieve this:
rules()
method:messages()
method:The key
articles.*.articles
ta…