diff --git a/src/fieldwork/Form.php b/src/fieldwork/Form.php index c3e8eb2..209dfcc 100644 --- a/src/fieldwork/Form.php +++ b/src/fieldwork/Form.php @@ -251,11 +251,23 @@ public function getScriptHTML () return $openingTag . $this->getScript() . $closingTag; } - public function renderFormError ($errorMsg) + static public function renderFormError ($errorMsg) { return sprintf("
%s
", $errorMsg); } + /** + * Get all the error messages for form-wide validators that returned invalid + */ + public function getFormErrors () + { + return array_map(function (FormValidator $formValidator) { + return $formValidator->getErrorMsg(); + }, array_filter($this->validators, function (FormValidator $validator) { + return !$validator->isValid(); + })); + } + /** * Gets the markup that is to be outputted before the actual contents of the form. This method could be used for * even more manual control over outputting with a custom markup. @@ -264,11 +276,9 @@ public function renderFormError ($errorMsg) public function getWrapBefore () { $dataFields = $this->getDataFieldsHTML(); - $errors = ''; - foreach ($this->validators as $validator) - /* @var $validator FormValidator */ - if (!$validator->isValid()) - $errors .= $this->renderFormError($validator->getErrorMsg()); + $errors = join(array_map(function ($errorMsg) { + return Form::renderFormError($errorMsg); + }, $this->getFormErrors())); return "
getAttributesString() . ">" . $errors . $dataFields; } @@ -469,7 +479,7 @@ function describeObject () /** * Gets a complete associated array containing all the data that needs to be stored * - * @param bool $useName Whether to use the field name (if not, the fields local slug is used) + * @param bool $useName Whether to use the field name (if not, the fields shorter local slug is used) * @param bool $includeDataFields Whether to include the data fields * * @return array @@ -521,4 +531,4 @@ public function getInnerHTML () { return parent::getHTML(); } -} \ No newline at end of file +}