From 2724d931112407facece00503cb175934c892007 Mon Sep 17 00:00:00 2001 From: shoosah Date: Fri, 22 Nov 2019 11:03:27 +1300 Subject: [PATCH] Allow to add error message into a specific field --- src/Forms/Form.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 38a9136d2..a88ed5728 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -1270,6 +1270,23 @@ class Form extends ViewableData implements HasRequestHandler $this->setSessionValidationResult($result); } + /** + * Set an error message for a field in the session, for display next time this form is shown. + * + * @param string $message the text of the message + * @param string $fieldName Name of the field to set the error message on it. + * @param string $type Should be set to good, bad, or warning. + * @param string|bool $cast Cast type; One of the CAST_ constant definitions. + * Bool values will be treated as plain text flag. + */ + public function sessionFieldError($message, $fieldName, $type = ValidationResult::TYPE_ERROR, $cast = ValidationResult::CAST_TEXT) + { + $this->setMessage($message, $type, $cast); + $result = $this->getSessionValidationResult() ?: ValidationResult::create(); + $result->addFieldMessage($fieldName, $message, $type, null, $cast); + $this->setSessionValidationResult($result); + } + /** * Returns the DataObject that has given this form its data * through {@link loadDataFrom()}.