I don't know if "form" OOP pattern exists but in our project using onPHP framework we have such class.
I really love to use it for validating and preprocessing any incoming data "from the world". If you don't use any framework maybe it's a good idea to start using any recognized one because most of them have model/form validation functionality.
Just an example for giving the idea:
/* @var $request HttpRequest */
$form = AccountFormFactory::getRegistrationForm()
->import($request->getPost())
->checkRules();
if ($form->getErrors()) {
// didn't pass validation, do something
...
}
And somewhere in AccountFormFactory::getRegistrationForm()
$form
->add(Primitive::string('email')->setPattern('~regex pattern~')->required())
->add(Primitive::string('password')->required())
->addMissingLabel('email', TranslationMarkers::REQUIRED_VALUE)
->addMissingLabel('password', TranslationMarkers::REQUIRED_VALUE)
->addWrongLabel('email', TranslationMarkers::INVALID_EMAIL)
->addWrongLabel('password', TranslationMarkers::INVALID_PASSWORD)
->addRule(
'uniqueEmail',
function (Form $form) {
$email = $form->exportValue('email');
if (User::dao()->findByEmail($newEmail)) {
$form
->markWrong('email')
->addWrongLabel('email', TranslationMarkers::DUPLICATE_EMAIL);
}
}
);