PHP OOP Design Patterns: Should I create two separate classes for registration and form validation?

I have two types of registration, registration A and registration B, each of which will have the same fields and several different fields. I was going to create an abstract class registration, and A and B will have their own classes that extend from registration.

Should I create a separate validation class with separate validation classes A and B? or is there a better pattern to use for something like that?

+4
source share
4 answers

I would accept registration and verification as separate entities.

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

+2
source

Create an abstract Validator class using the isValid () method and extend it.

Create a class account using the validate () method to pass the Validator object.

This will check something with any validator that you wrote.

+1
source

Yes, you probably need separate registration and verification objects. The proposed template, with an abstract base and ValidationA and ValidationB, which extends it, will work. If you really don't have a “default check”, you can just do an interface check and have ValidatingA and ValidatingB that implement it, and then you can use polymorphism at runtime to invoke the correct check method.

One thing that I began to understand is that you can ask and ask, discuss and discuss all this that you want, but you will never know until you try. Pick up a quick abstract base class and two that extend it and see if it does what you want. If not, just refactoring. Of course, you have to consider technical debt, etc., but just give it a chance - this is the best way to find out.

0
source

You can create your own Validator / ShowField class for each type of field (email, phone, login, password). You do this only once. Then you can create a Form class that will encapsulate all the standard "dirty" work with the form (field validation, form rendering, displaying validation messages if necessary, etc.).

Usage example:

$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 advantages:

  • You once create field checks.
  • The created Form class will be processed once.
  • All you do is define the form fields as shown below.
0
source

Source: https://habr.com/ru/post/1307783/


All Articles