Using rules on data not inside the form

Evening, I've want to make a field unique, but only within a subset. For example, if I am adding a Tag name, but I want that unique within a Type, but other Types could have the same name. I don't record the Type on the form, I add that to the DB using the ->mutateFormDataUsing method. So how do I make something like
->unique(modifyRuleUsing: function (Unique $rule, $data) {

return $rule
->where('cdt_type', $data('cdt_type'))

})
->unique(modifyRuleUsing: function (Unique $rule, $data) {

return $rule
->where('cdt_type', $data('cdt_type'))

})
Not that you can get $data in the unique() method, but cdt_type definitely not in $get
2 Replies
skyrowl
skyrowl2mo ago
I don't understand everything honestly so I might be wrong Tho it looks like you can add a hidden readonly field to get your type or add custom validation after the form submission If your custom validation fail you can throw a ValidationException with a custom message and it will show it in red like any field would for a ->required() for example
Matthew
MatthewOP2mo ago
Thanks for the suggestion. I thought about that, then you've still got to pass the data somewhere in CreateAction. My resolution was to actually something I should have thought of before, which was to go direct to get the information I previously wanted to pass, within the form: ->where('cdt_type', enumCdtType::from($livewire->activeTab)->value)
Want results from more Discord servers?
Add your server