dwightwatson / validating

Automatically validating Eloquent models for Laravel
MIT License
968 stars 76 forks source link

Inject current database row id on custom validation rule #117

Open Shingedo opened 9 years ago

Shingedo commented 9 years ago

This is kind of a follow up to issue #114 which lead to the fix of the injection of the current database rows id into laravels default unique validation rule. My problem is now, that I have a custom validation rule from another package felixkiss/uniquewith-validator called unique_with. This rule basically tests if the combination of a number of columns is unique, i. e. the combination of foreign_key_1, foreign_key_2 and foreign_key_3

'foreign_key_1' => 'unique_with:table,foreign_key_2,foreign_key_3'

This leads to the exact same issue as with laravels default unique rule when validation happens on updating: Of course the combination already exists. It exists on the model itself.

Thankfully the author of the package has thought about this as well and you append a row id as the last parameter to the rule to ignore it, just like laravels unique rule does it.

'foreign_key_1' => 'unique_with:table,foreign_key_2,foreign_key_3,<own_row_id>'

My problem is just how to achieve this while using the validating trait. The only solution I could come up with until now is adding a new config parameter and a new method that does all specified rules. This would basically be a generalisation of the current injectUniqueIdentifierToRules() and a configuration for it which by default replaces laravels own rule and if someone needs it like I do in this case they can add other rules to inject it into.

Would you be willing to add such an option?

dwightwatson commented 9 years ago

Yeah, I'd definitely be open to that functionality. I'm a little swamped at the moment but happy to accept a PR in the meantime.