Merge pull request #9332 from shoosah/feature/add-error-message-into-field

Allow to add error message into a specific field
This commit is contained in:
Andre Kiste 2020-05-01 15:46:29 +12:00 committed by GitHub
commit 3a424747df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions

View File

@ -1274,6 +1274,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()}.