Validates named form parameters against an expected type, presence and constraints, collecting field and form level error messages as it goes. Created via FormContext.newValidationContext, used by a controller to check a set of submitted values in sequence and then built into a JsonResult describing whether the submission was valid and, if not, the errors found.
Properties
| Property | Returns | Description |
|---|---|---|
| fieldMessages | List<FieldMessage> | The field level validation error messages recorded so far, one per addError(fieldName, message) call. |
| valid | boolean | Returns true if no validation errors have been found so far. |
Methods
isValid() · validateString(String fieldName, boolean required) · validateString(String fieldName, boolean required, int maxLength) · validateString(String fieldName, boolean required, int maxLength, Set<String> disallowedValues) · validateString(String fieldName, boolean required, int maxLength, Set<String> disallowedValues, String stringType) · validateRawString(String fieldName, boolean required) · validateRawString(String fieldName, boolean required, int maxLength) · validateRawString(String fieldName, boolean required, int maxLength, Set<String> disallowedValues) · validateLong(String fieldName, boolean required) · validateLong(String fieldName, boolean required, Long minValue, Long maxValue) · validateInteger(String fieldName, boolean required) · validateInteger(String fieldName, boolean required, Integer minValue, Integer maxValue) · validateDate(String fieldName, boolean required) · validateBoolean(String fieldName, boolean required) · validateBoolean(String fieldName, boolean required, boolean defaultValue) · validateDouble(String fieldName, boolean required) · validateBigDecimal(String fieldName, boolean required) · validateList(String fieldName, boolean required) · validateListLongs(String fieldName, boolean required) · validate(String fieldName, String type, boolean required) · validate(String fieldName, String type, boolean required, boolean strictMode) · validateFile(String name, boolean required) · addError(String fieldName, String message) · addError(String message) · getFieldMessages() · toJsonResult()
isValid()
Returns: boolean
Returns true if no validation errors have been found so far.
validateString(String fieldName, boolean required)
Returns: String
Validates the named parameter as a string, using this ValidationContext's strictMode, adding a field error if it is required but missing.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
validateString(String fieldName, boolean required, int maxLength)
Returns: String
Validates the named parameter as a string with a maximum length, adding a field error if it is required but missing or if it exceeds maxLength.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
maxLength | the maximum allowed length of the string value |
validateString(String fieldName, boolean required, int maxLength, Set<String> disallowedValues)
Returns: String
Validates the named parameter as a string with a maximum length and a set of disallowed values, adding a field error if it is required but missing, exceeds maxLength, or matches a disallowed value.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
maxLength | the maximum allowed length of the string value |
disallowedValues | values that are not permitted for this field, or null to allow any value |
validateString(String fieldName, boolean required, int maxLength, Set<String> disallowedValues, String stringType)
Returns: String
Validates the named parameter as a string of the given type, with a maximum length and a set of disallowed values, adding a field error if it is required but missing, exceeds maxLength, or matches a disallowed value.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
maxLength | the maximum allowed length of the string value |
disallowedValues | values that are not permitted for this field, or null to allow any value |
stringType | one of the TYPE_STRING or TYPE_RAW_STRING constants; defaults to TYPE_STRING if blank |
validateRawString(String fieldName, boolean required)
Returns: String
Validates the named parameter as an unsanitised raw string, adding a field error if it is required but missing.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
validateRawString(String fieldName, boolean required, int maxLength)
Returns: String
Validates the named parameter as an unsanitised raw string with a maximum length, adding a field error if it is required but missing or if it exceeds maxLength.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
maxLength | the maximum allowed length of the string value |
validateRawString(String fieldName, boolean required, int maxLength, Set<String> disallowedValues)
Returns: String
Validates the named parameter as an unsanitised raw string with a maximum length and a set of disallowed values, adding a field error if it is required but missing, exceeds maxLength, or matches a disallowed value.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
maxLength | the maximum allowed length of the string value |
disallowedValues | values that are not permitted for this field, or null to allow any value |
validateLong(String fieldName, boolean required)
Returns: Long
Validates the named parameter as a Long, adding a field error if it is required but missing or not a valid integer value.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
validateLong(String fieldName, boolean required, Long minValue, Long maxValue)
Returns: Long
Validates the named parameter as a Long within an optional range, adding a field error if it is required but missing, not a valid integer value, or outside the given bounds.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
minValue | the minimum allowed value, or null for no lower bound |
maxValue | the maximum allowed value, or null for no upper bound |
validateInteger(String fieldName, boolean required)
Returns: Integer
Validates the named parameter as an Integer, adding a field error if it is required but missing or not a valid integer value.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
validateInteger(String fieldName, boolean required, Integer minValue, Integer maxValue)
Returns: Integer
Validates the named parameter as an Integer within an optional range, adding a field error if it is required but missing, not a valid integer value, or outside the given bounds.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
minValue | the minimum allowed value, or null for no lower bound |
maxValue | the maximum allowed value, or null for no upper bound |
validateDate(String fieldName, boolean required)
Returns: Date
Validates the named parameter as a Date, adding a field error if it is required but missing or not a valid date value.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
validateBoolean(String fieldName, boolean required)
Returns: Boolean
Validates the named parameter as a Boolean, adding a field error if it is required but missing or not a recognised boolean value.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
validateBoolean(String fieldName, boolean required, boolean defaultValue)
Returns: Boolean
Validates the named parameter as a Boolean, falling back to the given default when it is missing or not a recognised boolean value. A field error is still added if the value is required but missing.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
defaultValue | the value to return if the parameter is missing or unparsable |
validateDouble(String fieldName, boolean required)
Returns: Double
Validates the named parameter as a Double, adding a field error if it is required but missing or not a valid decimal value.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
validateBigDecimal(String fieldName, boolean required)
Returns: BigDecimal
Validates the named parameter as a BigDecimal, adding a field error if it is required but missing or not a valid decimal value.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
validateList(String fieldName, boolean required)
Returns: List<String>
Validates the named parameter as a list of strings, adding a field error if it is required but missing.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
validateListLongs(String fieldName, boolean required)
Returns: List<Long>
Validates the named parameter as a list of Longs, adding a field error if it is required but missing.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
required | true to record an error if the value is missing |
validate(String fieldName, String type, boolean required)
Returns: Object
Checks the type and presence of the named form parameter, in strict mode. If the raw value contains disallowed content, cannot be parsed as the given type, or is required but missing, a field error message is added.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
type | one of the TYPE_* constant values naming the expected type |
required | true to record an error if the value is missing |
validate(String fieldName, String type, boolean required, boolean strictMode)
Returns: Object
Checks the type and presence of the named form parameter. If the raw value contains disallowed content (unless type is raw_string), cannot be parsed as the given type, or is required but missing, a field error message is added.
| Parameter | Description |
|---|---|
fieldName | the parameter name to validate |
type | one of the TYPE_* constant values naming the expected type |
required | true to record an error if the value is missing |
strictMode | when converting to a string, true to escape HTML entities in the cleaned value |
validateFile(String name, boolean required)
Returns: FileItem
Validates that the named uploaded file is present, adding a field error if it is required but missing.
| Parameter | Description |
|---|---|
name | the form field name the file was uploaded under |
required | true to record an error if the file is missing |
addError(String fieldName, String message)
Returns: ValidationContext
Adds a field level validation error message against the named field.
| Parameter | Description |
|---|---|
fieldName | the field the error applies to |
message | the error message to record |
addError(String message)
Returns: ValidationContext
Adds a form level (as opposed to field level) message indicating a validation failure
| Parameter | Description |
|---|---|
message | - the form level message to add |
getFieldMessages()
Returns: List<FieldMessage>
The field level validation error messages recorded so far, one per addError(fieldName, message) call.
toJsonResult()
Returns: JsonResult
Builds a JsonResult summarising this ValidationContext's outcome. If any field or form level errors have been recorded, returns a failed JsonResult carrying those messages; otherwise returns a successful JsonResult.