Skip to content

Commit 8c266c0

Browse files
committed
Add #validateAnswerConstraint to FormCOntroller
1 parent 839513c commit 8c266c0

File tree

5 files changed

+29
-22
lines changed

5 files changed

+29
-22
lines changed

collect_app/src/main/java/org/odk/collect/android/formentry/FormEntryViewModel.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.odk.collect.android.javarosawrapper.FailedValidationResult;
3131
import org.odk.collect.android.javarosawrapper.FormController;
3232
import org.odk.collect.android.javarosawrapper.RepeatsInFieldListException;
33-
import org.odk.collect.android.javarosawrapper.SuccessValidationResult;
3433
import org.odk.collect.android.javarosawrapper.ValidationResult;
3534
import org.odk.collect.android.logic.ImmutableDisplayableQuestion;
3635
import org.odk.collect.android.utilities.Appearances;
@@ -394,12 +393,8 @@ public void exit() {
394393

395394
public void validateAnswerConstraint(FormIndex index, IAnswerData answer) {
396395
worker.immediate(() -> {
397-
boolean isAnswerValid = formController.getFormDef().evaluateConstraint(index.getReference(), answer);
398-
if (isAnswerValid) {
399-
validationResult.postValue(new Consumable<>(SuccessValidationResult.INSTANCE));
400-
} else {
401-
validationResult.postValue(new Consumable<>(formController.getFailedValidationResult(index, FormEntryController.ANSWER_CONSTRAINT_VIOLATED)));
402-
}
396+
ValidationResult result = formController.validateAnswerConstraint(index, answer);
397+
validationResult.postValue(new Consumable<>(result));
403398
});
404399
}
405400

collect_app/src/main/java/org/odk/collect/android/javarosawrapper/FormController.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ interface FormController {
129129
@Throws(JavaRosaException::class)
130130
fun answerQuestion(index: FormIndex?, data: IAnswerData?): Int
131131

132+
/**
133+
* Validates a single answer against its constraint.
134+
*
135+
* @return SuccessValidationResult if the answer is valid, or an error result describing
136+
* the violated constraint.
137+
*/
138+
fun validateAnswerConstraint(index: FormIndex, answer: IAnswerData?): ValidationResult
139+
132140
/**
133141
* Goes through the entire form to make sure all entered answers comply with their constraints.
134142
* Constraints are ignored on 'jump to', so answers can be outside of constraints. We don't
@@ -140,8 +148,6 @@ interface FormController {
140148
@Throws(JavaRosaException::class)
141149
fun validateAnswers(moveToInvalidIndex: Boolean): ValidationResult
142150

143-
fun getFailedValidationResult(index: FormIndex, status: Int): ValidationResult
144-
145151
/**
146152
* saveAnswer attempts to save the current answer into the data model without doing any
147153
* constraint checking. Only use this if you know what you're doing. For normal form filling

collect_app/src/main/java/org/odk/collect/android/javarosawrapper/JavaRosaFormController.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,15 @@ public int answerQuestion(FormIndex index, IAnswerData data) throws JavaRosaExce
393393
}
394394
}
395395

396+
public ValidationResult validateAnswerConstraint(FormIndex index, IAnswerData answer) {
397+
boolean isAnswerValid = getFormDef().evaluateConstraint(index.getReference(), answer);
398+
if (isAnswerValid) {
399+
return SuccessValidationResult.INSTANCE;
400+
} else {
401+
return getFailedValidationResult(index, FormEntryController.ANSWER_CONSTRAINT_VIOLATED);
402+
}
403+
}
404+
396405
public ValidationResult validateAnswers(boolean moveToInvalidIndex) throws JavaRosaException {
397406
try {
398407
ValidateOutcome validateOutcome = getFormDef().validate();
@@ -411,8 +420,7 @@ public ValidationResult validateAnswers(boolean moveToInvalidIndex) throws JavaR
411420
}
412421
}
413422

414-
@Override
415-
public ValidationResult getFailedValidationResult(FormIndex index, int status) {
423+
private ValidationResult getFailedValidationResult(FormIndex index, int status) {
416424
ValidationResult validationResult = null;
417425

418426
String errorMessage;

collect_app/src/test/java/org/odk/collect/android/javarosawrapper/FakeFormController.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,20 @@ public FormEntryPrompt getQuestionPrompt() {
129129
return currentPrompts.get(0);
130130
}
131131

132-
@NonNull
133132
@Override
134-
public ValidationResult validateAnswers(boolean moveToInvalidIndex) throws JavaRosaException {
135-
if (validationError != null) {
136-
throw validationError;
133+
public @NotNull ValidationResult validateAnswerConstraint(@NotNull FormIndex index, @Nullable IAnswerData answer) {
134+
if (failedConstraint != null) {
135+
return failedConstraint;
137136
} else {
138137
return SuccessValidationResult.INSTANCE;
139138
}
140139
}
141140

141+
@NonNull
142142
@Override
143-
public @NotNull ValidationResult getFailedValidationResult(@NotNull FormIndex index, int status) {
144-
if (failedConstraint != null) {
145-
return failedConstraint;
143+
public ValidationResult validateAnswers(boolean moveToInvalidIndex) throws JavaRosaException {
144+
if (validationError != null) {
145+
throw validationError;
146146
} else {
147147
return SuccessValidationResult.INSTANCE;
148148
}

collect_app/src/test/java/org/odk/collect/android/utilities/StubFormController.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ open class StubFormController : FormController {
7575

7676
override fun answerQuestion(index: FormIndex?, data: IAnswerData?): Int = -1
7777

78+
override fun validateAnswerConstraint(index: FormIndex, answer: IAnswerData?): ValidationResult = SuccessValidationResult
79+
7880
@Throws(JavaRosaException::class)
7981
override fun validateAnswers(moveToInvalidIndex: Boolean): ValidationResult = SuccessValidationResult
8082

81-
override fun getFailedValidationResult(index: FormIndex, status: Int): ValidationResult {
82-
return SuccessValidationResult
83-
}
84-
8583
override fun saveAnswer(index: FormIndex?, data: IAnswerData?): Boolean = false
8684

8785
override fun stepToNextEvent(stepIntoGroup: Boolean): Int = -1

0 commit comments

Comments
 (0)