-
Notifications
You must be signed in to change notification settings - Fork 6
validation via the SDK #267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7d1ea98
expect a formatted value to be UTC
5a0af26
make BaseFieldState generic
939cd80
make DateTimeFieldState vary with Instant?
b1b5250
first pass at consuming core validation errors
eaf6ff7
factor out Validation States to a common file
2fc16dd
Merge branch 'feature-branches/forms' into sor10874/validate
cb9fb10
Merge branch 'feature-branches/forms' into sor10874/validate
a41b29a
remove merge detritus
896c989
update SDK.
042985b
Merge branch 'feature-branches/forms' into sor10874/validate
3a470e6
cleanup
118c383
remove unused import. simplify syntax.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...orms/src/main/java/com/arcgismaps/toolkit/featureforms/components/base/FieldValidation.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* COPYRIGHT 1995-2023 ESRI | ||
* | ||
* TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL | ||
* Unpublished material - all rights reserved under the | ||
* Copyright Laws of the United States. | ||
* | ||
* For additional information, contact: | ||
* Environmental Systems Research Institute, Inc. | ||
* Attn: Contracts Dept | ||
* 380 New York Street | ||
* Redlands, California, USA 92373 | ||
* | ||
* email: [email protected] | ||
*/ | ||
|
||
package com.arcgismaps.toolkit.featureforms.components.base | ||
|
||
internal sealed class ValidationErrorState { | ||
object NoError: ValidationErrorState() | ||
object Required: ValidationErrorState() | ||
object MinMaxCharConstraint: ValidationErrorState() | ||
object ExactCharConstraint: ValidationErrorState() | ||
object MaxCharConstraint: ValidationErrorState() | ||
object MinNumericConstraint: ValidationErrorState() | ||
object MaxNumericConstraint: ValidationErrorState() | ||
object MinMaxNumericConstraint: ValidationErrorState() | ||
object NotANumber: ValidationErrorState() | ||
object NotAWholeNumber: ValidationErrorState() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,4 +148,3 @@ internal fun rememberDateTimeFieldState( | |
} | ||
) | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -28,24 +28,26 @@ import androidx.compose.runtime.saveable.rememberSaveable | |||||
import com.arcgismaps.data.Domain | ||||||
import com.arcgismaps.data.FieldType | ||||||
import com.arcgismaps.data.RangeDomain | ||||||
import com.arcgismaps.exceptions.FeatureFormValidationException | ||||||
import com.arcgismaps.mapping.featureforms.FeatureForm | ||||||
import com.arcgismaps.mapping.featureforms.FieldFormElement | ||||||
import com.arcgismaps.mapping.featureforms.TextAreaFormInput | ||||||
import com.arcgismaps.mapping.featureforms.TextBoxFormInput | ||||||
import com.arcgismaps.toolkit.featureforms.R | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.BaseFieldState | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.FieldProperties | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState.ExactCharConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState.MaxCharConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState.MaxNumericConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState.MinMaxCharConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState.MinMaxNumericConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState.MinNumericConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState.NoError | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState.NotANumber | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState.NotAWholeNumber | ||||||
import com.arcgismaps.toolkit.featureforms.components.base.ValidationErrorState.Required | ||||||
import com.arcgismaps.toolkit.featureforms.components.formelement.FieldElement | ||||||
import com.arcgismaps.toolkit.featureforms.components.text.ValidationErrorState.ExactCharConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.text.ValidationErrorState.MaxCharConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.text.ValidationErrorState.MaxNumericConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.text.ValidationErrorState.MinMaxCharConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.text.ValidationErrorState.MinMaxNumericConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.text.ValidationErrorState.MinNumericConstraint | ||||||
import com.arcgismaps.toolkit.featureforms.components.text.ValidationErrorState.NoError | ||||||
import com.arcgismaps.toolkit.featureforms.components.text.ValidationErrorState.NotANumber | ||||||
import com.arcgismaps.toolkit.featureforms.components.text.ValidationErrorState.NotAWholeNumber | ||||||
import com.arcgismaps.toolkit.featureforms.components.text.ValidationErrorState.Required | ||||||
import com.arcgismaps.toolkit.featureforms.utils.asDoubleTuple | ||||||
import com.arcgismaps.toolkit.featureforms.utils.asLongTuple | ||||||
import com.arcgismaps.toolkit.featureforms.utils.domain | ||||||
|
@@ -95,12 +97,14 @@ internal class FormTextFieldState( | |||||
initialValue: String = properties.value.value, | ||||||
scope: CoroutineScope, | ||||||
private val context: Context, | ||||||
onEditValue: (Any?) -> Unit | ||||||
onEditValue: (Any?) -> Unit, | ||||||
validate: () -> List<Throwable> | ||||||
) : BaseFieldState<String>( | ||||||
properties = properties, | ||||||
initialValue = initialValue, | ||||||
scope = scope, | ||||||
onEditValue = onEditValue | ||||||
onEditValue = onEditValue, | ||||||
validate = validate | ||||||
) { | ||||||
// indicates singleLine only if TextBoxFeatureFormInput | ||||||
val singleLine = properties.singleLine | ||||||
|
@@ -125,7 +129,6 @@ internal class FormTextFieldState( | |||||
private val _hasError = mutableStateOf(false) | ||||||
private val _supportingTextIsErrorMessage = mutableStateOf(false) | ||||||
val supportingTextIsErrorMessage: State<Boolean> = _supportingTextIsErrorMessage | ||||||
|
||||||
/** | ||||||
* The domain of the element's field. | ||||||
*/ | ||||||
|
@@ -236,38 +239,6 @@ internal class FormTextFieldState( | |||||
_hasError.value = errors.isNotEmpty() | ||||||
} | ||||||
|
||||||
private fun validateTextRange(value: String): ValidationErrorState = | ||||||
if (value.length !in minLength..maxLength) { | ||||||
if (minLength > 0 && maxLength > 0) { | ||||||
if (minLength == maxLength) { | ||||||
ExactCharConstraint | ||||||
} else { | ||||||
MinMaxCharConstraint | ||||||
} | ||||||
} else { | ||||||
MaxCharConstraint | ||||||
} | ||||||
} else { | ||||||
NoError | ||||||
} | ||||||
|
||||||
private fun validateNumber(value: String): ValidationErrorState = | ||||||
if (fieldType.isIntegerType) { | ||||||
val numberVal = value.toIntOrNull() | ||||||
if (numberVal == null) { | ||||||
NotAWholeNumber | ||||||
} else { | ||||||
validateNumericRange(numberVal) | ||||||
} | ||||||
} else { | ||||||
val numberVal = value.toDoubleOrNull() | ||||||
if (numberVal == null) { | ||||||
NotANumber | ||||||
} else { | ||||||
validateNumericRange(numberVal) | ||||||
} | ||||||
} | ||||||
|
||||||
private fun validateNumericRange(numberVal: Int): ValidationErrorState { | ||||||
require(fieldType.isIntegerType) | ||||||
return if (domain != null && domain is RangeDomain) { | ||||||
|
@@ -340,7 +311,6 @@ internal class FormTextFieldState( | |||||
validationErrors.firstOrNull { it != Required && it != NotANumber && it != NotAWholeNumber } ?: NoError | ||||||
} else { | ||||||
// if non empty, focused, show any error other than required (the Required error shouldn't be in the list) | ||||||
check (!validationErrors.contains(Required)) | ||||||
validationErrors.first() | ||||||
} | ||||||
} else if (hasBeenFocused) { | ||||||
|
@@ -354,32 +324,58 @@ internal class FormTextFieldState( | |||||
} | ||||||
} else { | ||||||
// if non empty, unfocused, show any error other than required (the Required error shouldn't be in the list) | ||||||
check (!validationErrors.contains(Required)) | ||||||
validationErrors.first() | ||||||
} | ||||||
} else { | ||||||
// never been focused | ||||||
NoError | ||||||
} | ||||||
|
||||||
private fun validate(value: String): MutableList<ValidationErrorState> { | ||||||
private fun validate(value: String): List<ValidationErrorState> { | ||||||
val errors = validate.invoke() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this can be a simple call. |
||||||
val ret = mutableListOf<ValidationErrorState>() | ||||||
if (isRequired.value && value.isEmpty()) { | ||||||
if (errors.any { it is FeatureFormValidationException.RequiredException }) { | ||||||
ret += Required | ||||||
} | ||||||
|
||||||
if (!fieldType.isNumeric) { | ||||||
val rangeError = validateTextRange(value) | ||||||
if (rangeError != NoError) { | ||||||
ret += rangeError | ||||||
if (errors.any { it is FeatureFormValidationException.MinCharConstraintException } | ||||||
|| errors.any { it is FeatureFormValidationException.MaxCharConstraintException } | ||||||
) { | ||||||
if (minLength > 0 && maxLength > 0) { | ||||||
if (minLength == maxLength) { | ||||||
ret += ExactCharConstraint | ||||||
} else { | ||||||
ret += MinMaxCharConstraint | ||||||
} | ||||||
} else { | ||||||
ret += MaxCharConstraint | ||||||
} | ||||||
} | ||||||
} else { | ||||||
val error = validateNumber(value) | ||||||
if (error != NoError) { | ||||||
ret += error | ||||||
if (fieldType.isIntegerType) { | ||||||
val numberVal = value.toIntOrNull() | ||||||
if (numberVal == null) { | ||||||
ret += NotAWholeNumber | ||||||
} else { | ||||||
val error = validateNumericRange(numberVal) | ||||||
if (error != NoError) { | ||||||
ret += error | ||||||
} | ||||||
} | ||||||
} else { | ||||||
val numberVal = value.toDoubleOrNull() | ||||||
if (numberVal == null) { | ||||||
ret += NotANumber | ||||||
} else { | ||||||
val error = validateNumericRange(numberVal) | ||||||
if (error != NoError) { | ||||||
ret += error | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return ret | ||||||
} | ||||||
|
||||||
|
@@ -425,6 +421,7 @@ internal class FormTextFieldState( | |||||
form.editValue(formElement, newValue) | ||||||
scope.launch { form.evaluateExpressions() } | ||||||
}, | ||||||
validate = { formElement.getValidationErrors() } | ||||||
).apply { | ||||||
// focus is lost on rotation. https://devtopia.esri.com/runtime/apollo/issues/230 | ||||||
hasBeenFocused = list[1] as Boolean | ||||||
|
@@ -459,30 +456,18 @@ internal fun rememberFormTextFieldState( | |||||
fieldType = form.fieldType(field), | ||||||
domain = form.domain(field) as? RangeDomain, | ||||||
minLength = minLength, | ||||||
maxLength = maxLength, | ||||||
maxLength = maxLength | ||||||
), | ||||||
scope = scope, | ||||||
context = context, | ||||||
onEditValue = { | ||||||
form.editValue(field, it) | ||||||
scope.launch { form.evaluateExpressions() } | ||||||
} | ||||||
}, | ||||||
validate = { field.getValidationErrors() } | ||||||
) | ||||||
} | ||||||
|
||||||
private sealed class ValidationErrorState { | ||||||
object NoError: ValidationErrorState() | ||||||
object Required: ValidationErrorState() | ||||||
object MinMaxCharConstraint: ValidationErrorState() | ||||||
object ExactCharConstraint: ValidationErrorState() | ||||||
object MaxCharConstraint: ValidationErrorState() | ||||||
object MinNumericConstraint: ValidationErrorState() | ||||||
object MaxNumericConstraint: ValidationErrorState() | ||||||
object MinMaxNumericConstraint: ValidationErrorState() | ||||||
object NotANumber: ValidationErrorState() | ||||||
object NotAWholeNumber: ValidationErrorState() | ||||||
} | ||||||
|
||||||
/** | ||||||
* Provide a format string for any numeric type. | ||||||
* | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.