What is the cleanest way to split validation logic into multiple files
I have a really big model with about 200 fields. I want to make a validation for this model, which is pretty complex. If I do it in a single file, this will produce a thousand line long file that I want to avoid.
My base idea is, to split the validation logic into multiple files based on the fields, so I have a validator class for every field of the model. Which is the cleanest way to do it without hacking?
I believe it could be done, If I could define a Rule for every field, that rule combines other predefined rules, but I do not find a way to do this. Could somebody help? Thank you in advance!
If I put the whole code inside one FormRequest, that will be huge.
If I put everything inside one rule per field, then I have to merge all logic into one method, and I cannot use the predefined ones (like, required, min ...etc).
If I split the code into some custom rules, but add the predefined one also to the FormRequest then my code becomes segmented, and have to find bugs in multiple files if there is a problem.
So what I want is, to separate the validation logic into multiple files, but keep the relevant parts together that belongs to one specific field. I hope you understand my problem.
@gitwithravish Btw I am working on an educational management system for my university, and the problematic model is the Subject model, which has many important fields like name, credit, supervisor, organization ...etc, and I have to implement all the relevant validation logic defined in hundreds of pages.
I don't understand why this is a "problem" anyway !
So what I want is, to separate the validation logic into multiple files, but keep the relevant parts together that belongs to one specific field. I hope you understand my problem.
That is exactly what custom rule is for.
Do you have some code that you want to use in multiple custom rules without duplicating ?
Can you give actual example from your project that's what exactly is bothering you @kisgaben ?
@kisgaben If you have a 200-field model, do you really thing splitting rules into a file per rule (so 200 files with fragmented validation logic) is really the “cleanest” way…?