views:

285

answers:

4

So here's my problem: I have two types of registration, registration A and registration B, each will have some of the same fields and some different fields. I was going to create abstract class registration and both A and B would have their own classes that extend from registration. My question is, should I create a seperate Validation class with seperate A and B validation classes that extend? or is there a better pattern to use for something like this?

Thanks

+1  A: 

I would take Registration and Validation as separate entities yes.

Edit: Also, this SO question might contain some valuable information for you.

Ropstah
This is a nice site which shows how to build an extensible form validation class: http://www.devshed.com/c/a/PHP/Building-An-Extensible-Form-Validator-Class/
Ropstah
A: 

Yes, you probably want separate registration and validation entities. The pattern you suggested, with an abstract base and a ValidationA and ValidationB that extends it would work. If you don't really have a "default" validation, you could just make Validating an interface, and have ValidatingA and ValidatingB that implement it, and then you'd be able to use runtime polymorphism to call the proper Validate method.

One thing I've begun to realize is that you can ask and ask and discuss and muse over this kind of thing all you want, but you'll never know until you try it. Whip up a quick abstract base class and two that extend it and see if it accomplishes what you want. If not, just refactor. Sure you have to consider technical debt, etc., but just give it a shot - it's the best way to learn.

awshepard
Yes, the only thing is, some of the fields will be validated the same for each type A and B, so I am going to create the base validator class abstract and extend from it as needed.
ThinkingInBits
A: 

Create a Validator abstract class with a method 'isValid()' and extend to your need.

Create a class Registration with a method validate() taking Validator object to pass through.

It'll allow to validate anything with any validator you wrote.

Boris Guéry
Thanks, I'm actually having multiple validation methods within the base validate class since each A and B will both have some of the same validations IE. validateUsername. Upon extending, I'll create new type specific validation methods.
ThinkingInBits
A: 

You can create its own Validator/ShowField class for every field type (e-mail, phone, login, password). You do it only once. Then you can create a Form class that will encapsulate all standard "dirty" work with the form (validating fields, showing form, showing validation messages if needed etc.)

Example of usage:

$form = new Form($action, $method); // $action and $method - html attributes of the form
$form->addField('Your e-mail', 'mail'); // field label (for the form) and field type (for the form and for validation)
$form->addField('Login', 'login');

$form->process(); // show form to the user or process it if already submited and sent

Your benefits:

  1. You create field validators once.
  2. You create Form class will all form processing once.
  3. All you do is define form fields as shown below.
topright