patternphpMinor
Validator classes
Viewed 0 times
validatorclassesstackoverflow
Problem
We're trying to design validation classes with a common interface. This would apply to all validators. We would define a validator interface with a
Option 1: Instantiate a validator object (either directly or through a factory) to perform validation
Option 2: Validator interface defines a static
Which would be the best option to use? Are there better ways to go about this than those laid out above?
validate($input):bool method. Each validator would represent a specific data type (email address, login, etc.); the validate method would implement the appropriate logic.Option 1: Instantiate a validator object (either directly or through a factory) to perform validation
class EmailValidator implements ValidatorInterface
{
public function validate($input) { ... }
}
// Usage:
$var = new EmailValidator();
if ($var->validate($val)) { ... }Option 2: Validator interface defines a static
validate() method implemented by each validatorclass EmailValidator implements ValidatorInterface
{
public static function validate($input) { ... }
}
// Usage:
if (EmailValidator::validate($val)) { ... }Which would be the best option to use? Are there better ways to go about this than those laid out above?
Solution
Don't use static methods. Eventually you will want to be able to apply a group of validators in a single operation i.e. testing for NonBlank then testing for Email. Easy to do with objects, far more challenging with statics.
It's interesting that your Validator is very similar to that used by the popular Symfony\Validator component. So I think it's safe to say you are on the right track.
Might want to take a look at the component just to get some ideas:
https://github.com/symfony/Validator
http://symfony.com/doc/current/book/validation.html
=================================================================
I don't understand why it would be more challenging with statics.
If you needed multiple constraints then creating instances, storing them in an array and then looping over the array is easier then using statics.
But lets consider a different problem. Suppose you want a UniqueEmailValidator which needs access to the database. It's a bit awkward using statics but:
And once you start using a dependency injection container you can do:
Which hides the $db portion.
It's interesting that your Validator is very similar to that used by the popular Symfony\Validator component. So I think it's safe to say you are on the right track.
Might want to take a look at the component just to get some ideas:
https://github.com/symfony/Validator
http://symfony.com/doc/current/book/validation.html
=================================================================
I don't understand why it would be more challenging with statics.
If you needed multiple constraints then creating instances, storing them in an array and then looping over the array is easier then using statics.
But lets consider a different problem. Suppose you want a UniqueEmailValidator which needs access to the database. It's a bit awkward using statics but:
$validator = new UniqueEmailValidator($db); // Works wellAnd once you start using a dependency injection container you can do:
$validator = $container->getService('unique_email_validator');Which hides the $db portion.
Code Snippets
$validator = new UniqueEmailValidator($db); // Works well$validator = $container->getService('unique_email_validator');Context
StackExchange Code Review Q#30305, answer score: 6
Revisions (0)
No revisions yet.