klein / klein.php

A fast & flexible router
MIT License
2.66k stars 290 forks source link

Using Validators Outside of Routes #270

Closed thomasdashney closed 9 years ago

thomasdashney commented 9 years ago

Hi, first of all I love Klein!

Question regarding the Validators: I have Models in my application defined in their own classes. I understand that the validators are accessed via the $service provider. Is there any way for me to include the validators and use them (statically) within other classes, such as my model classes?

Cheers

Rican7 commented 9 years ago

Whoops! Fat fingered those tags there. :P I'd love to help!

The validator class can be used externally, but I wouldn't suggest it. To be honest, the validators are most likely not going to stay in Klein past 2.x. While I understand that they're very convenient, they are not a part of Klein's main concern as a library and introduce overhead in moving the project forward (and testing), not to mention that they aren't as robust as they could be.

Instead, I'd suggest using a more robust and well-tested validation library. Here are a few that I'd suggest:

If you still realllly want to use the Klein Validations for some reason, it is possible.... The Klein\ Validator class holds its validators statically, and only initializes once, so you could have multiple instances of the Validator class without issue, as the actual validation rules are all held in a static/global context. That means that you could either inject the Validator into your model classes (recommended) or create new instances of the Validator class in each scope that you need it. Make sense?

I hope I was of help! I really appreciate the high praise of Klein! I love working on it, haha. :) I'm sorry if the info about the Klein Validator is disheartening, but I'd rather keep the project realllly good at what its designed to do and defer to other packages for other responsibilities. :smiley:

thomasdashney commented 9 years ago

Thanks for the response!

I definitely agree that it's a good idea to keep Klein.php as a "routing" library rather than a jack of all trades. I'll take a look at those other libraries, thanks a ton for the suggestion!

Rican7 commented 9 years ago

Of course! Thank you for understanding!

Let me know if you need anymore help! :)

doctorallen commented 9 years ago

+1 for Illuminate/Validation