So I've been using the Valitron Library to validate posted forms mostly and have run into some issues.
The constructor accepts the data to be validated and this causes problems when you inject the library as a dependency with Pimple or some other container. It also causes issues if you want to validate multiple things, as you basically have to instantiate the library every time you want to use it.
Is there some way around this?
Ultimately I would like to be able to define the library as a service and inject it with Pimple like this:
$container['Valitron'] = function(){
return new \Valitron\Validator();
};
Any controller/class that needs to validate something would initialise it in their constructor like this:
public function __construct($valitron)
{
$this->valitron = $valitron;
}
Any time I need to validate something I could say something like:
// First use
$this->valitron->setData($_POST);
$this->valitron->rule('required', 'name')->message('Im required')->label('Name');
$this->valitron->validate();
// Second use
$this->valitron->setData($_GET);
$this->valitron->rule('required', 'test')->message('Im also required')->label('Test');
$this->valitron->validate();
But there doesn't seem to be a setData function, or any way to reset the library between usages.
Question: How do I use Valitron with Pimple and reuse it for validating multiple things at a time?
Please Note: It must be injected. It also should not need to be initialised before each usage. Please don't tell me I have to extend the library or hack it to make it work properly!