rubenski Posted February 3, 2010 Share Posted February 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/190796-your-thoughts-on-this-design-issue/ Share on other sites More sharing options...
KevinM1 Posted February 3, 2010 Share Posted February 3, 2010 What do the different concrete validators do? How would you normally invoke their validation methods? Quote Link to comment https://forums.phpfreaks.com/topic/190796-your-thoughts-on-this-design-issue/#findComment-1006142 Share on other sites More sharing options...
ignace Posted February 3, 2010 Share Posted February 3, 2010 It is indeed not good but why not do: $validator = FormElementValidator::findValidator($formElement); if ($validator && $validator->isVaild($formElement)) { Which can be used for each validator Quote Link to comment https://forums.phpfreaks.com/topic/190796-your-thoughts-on-this-design-issue/#findComment-1006144 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.