Jump to content

Validation. Should I use different classes for each rule, or methods?


airportmarc

Recommended Posts

Hello all,

 

This is more a design question.  I am rebuilding my validation class, and have looked at a few different frameworks to see how they do it.  What I see is that they create a base class, then each validation method extends this.  eg. Required is a Class that extends Validate.  Is there a benefit to doing this, compared to have a single class with a required method?

 

Any insight would be great.

 

 

 

 

Link to comment
Share on other sites

It does mean that you have a standard interface. With all validations being done via the same method. This way you can do things such as:

 

$validators = Validator::get();
foreach ($validators as $validator) {
  $str = $validator->validate($str);
}

 

Doing it with a separate method for each you would need to know all the different method names at runtime.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.