/** * Validates a field. * * @param \FormFieldModel $formField * @param int $step * * @return bool */ public function validateField(\FormFieldModel $formField, $step) { $class = $GLOBALS['TL_FFL'][$formField->type]; if (!class_exists($class)) { return true; } /** @var \Widget $widget */ $widget = new $class($formField->row()); $widget->required = $formField->mandatory ? true : false; // Needed for the hook $form = $this->createDummyForm(); // HOOK: load form field callback if (isset($GLOBALS['TL_HOOKS']['loadFormField']) && is_array($GLOBALS['TL_HOOKS']['loadFormField'])) { foreach ($GLOBALS['TL_HOOKS']['loadFormField'] as $callback) { $objCallback = \System::importStatic($callback[0]); $widget = $objCallback->{$callback[1]}($widget, $this->getFormId(), $this->formModel->row(), $form); } } // Validation (needs to set POST values because the widget class searches // only in POST values :-( // This should only happen if value is not currently submitted and if // the value is neither submitted in POST nor in the session, we have // to default it to an empty string so the widget validates for mandatory // fields $fakeValidation = false; if (!isset($_POST[$widget->name])) { if ($this->isStoredInData($widget->name, $step)) { $value = $this->fetchFromData($widget->name, $step); } else { $value = ''; } \Input::setPost($formField->name, $value); $fakeValidation = true; } $widget->validate(); // Reset fake validation if ($fakeValidation) { \Input::setPost($formField->name, null); } // Special hack for upload fields because they delete $_FILES and thus // multiple validation calls will fail - sigh if ($widget instanceof \uploadable && isset($_SESSION['FILES'][$widget->name])) { $_FILES[$widget->name] = $_SESSION['FILES'][$widget->name]; } // HOOK: validate form field callback if (isset($GLOBALS['TL_HOOKS']['validateFormField']) && is_array($GLOBALS['TL_HOOKS']['validateFormField'])) { foreach ($GLOBALS['TL_HOOKS']['validateFormField'] as $callback) { $objCallback = \System::importStatic($callback[0]); $widget = $objCallback->{$callback[1]}($widget, $this->getFormId(), $this->formModel->row(), $form); } } return !$widget->hasErrors(); }