Jump to content

Recommended Posts

Hi,

 

I think I ran into a design problem with the PHP application I'm building.

 

I have this function called validateFormElement() that takes FormElements polymorphically. A FormElement can be a FormElement or a TextFieldFormElement, or a TextAreaFormElement, etc. Inside the method I use a factory to obtain the proper FormElementValidator. Passing a TextFieldFormElement to the validateFormElement() function will make the factory return the specific TextFieldFormElementValidator.

 

 

function validateFormElement(FormElement $formElement){
$validator = FormElementValidatorFactory::getInstance(get_class($formElement));

// .. if-elsing here
} 

 

But now I don't know what functions I can call on the $validator. Each type of Validator returned by the factory has different functions and now I find myself if-elsing to see what type of $validator I have and consequently what functions I can call on $validator.

 

This is not good. Is it?

 

How would you do this? I need a specific validator for each type of $formElement. The issue is not that I can't get it to work. If-elsing does the job, but it just seems... wrong.

 

 

Link to comment
https://forums.phpfreaks.com/topic/190796-your-thoughts-on-this-design-issue/
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.