Search in sources :

Example 6 with FieldInput

use of org.activityinfo.ui.client.input.model.FieldInput in project activityinfo by bedatadriven.

the class FormInputViewModelTest method inputMask.

@Test
public void inputMask() {
    IntakeForm intakeForm = setup.getCatalog().getIntakeForm();
    FormInputViewModelBuilder builder = builderFor(intakeForm);
    FormInputModel inputModel = new FormInputModel(new RecordRef(intakeForm.getFormId(), ResourceId.generateId()));
    // Fill in required fields
    inputModel = inputModel.update(intakeForm.getOpenDateFieldId(), new LocalDate(2017, 1, 1));
    // Does not match input mask "000"
    inputModel = inputModel.update(intakeForm.getRegNumberFieldId(), new FieldInput(TextValue.valueOf("FOOOOO")));
    FormInputViewModel viewModel = builder.build(inputModel);
    assertThat(viewModel.isValid(), equalTo(false));
    assertThat(viewModel.getValidationErrors(intakeForm.getRegNumberFieldId()), not(empty()));
}
Also used : RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) LocalDate(org.activityinfo.model.type.time.LocalDate) Test(org.junit.Test)

Example 7 with FieldInput

use of org.activityinfo.ui.client.input.model.FieldInput in project activityinfo by bedatadriven.

the class AbstractWeekWidget method onDatePicked.

private void onDatePicked(ValueChangeEvent<Date> event) {
    LocalDate date = new LocalDate(event.getValue());
    dateMenu.hide();
    updater.update(new FieldInput(periodType.containingDate(date)));
}
Also used : FieldInput(org.activityinfo.ui.client.input.model.FieldInput) LocalDate(org.activityinfo.model.type.time.LocalDate)

Example 8 with FieldInput

use of org.activityinfo.ui.client.input.model.FieldInput in project activityinfo by bedatadriven.

the class FormInputViewModel method buildUpdate.

public RecordUpdate buildUpdate(Optional<RecordRef> parentRef) {
    RecordUpdate update = new RecordUpdate();
    update.setRecordId(inputModel.getRecordRef().getRecordId());
    update.setFormId(inputModel.getRecordRef().getFormId());
    if (parentRef.isPresent()) {
        update.setParentRecordId(parentRef.get().getRecordId().asString());
    }
    for (FormTree.Node node : formTree.getRootFields()) {
        FieldInput newInput = inputModel.get(node.getFieldId());
        if (newInput.getState() == FieldInput.State.VALID) {
            update.setFieldValue(node.getFieldId(), newInput.getValue());
        } else if (existingValues.containsKey(node.getFieldId()) && newInput.getState() == FieldInput.State.EMPTY) {
            update.setFieldValue(node.getFieldId().asString(), Json.createNull());
        } else if (existingValues.containsKey(node.getFieldId()) && !relevant.contains(node.getFieldId())) {
            update.setFieldValue(node.getFieldId().asString(), Json.createNull());
        }
    }
    return update;
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) FormTree(org.activityinfo.model.formTree.FormTree) FieldInput(org.activityinfo.ui.client.input.model.FieldInput)

Example 9 with FieldInput

use of org.activityinfo.ui.client.input.model.FieldInput in project activityinfo by bedatadriven.

the class FormInputViewModelBuilder method computeUpdatedRecord.

/**
 * Computes the effective record, based
 * @param existingValues
 * @param inputModel
 * @return
 */
private FormInstance computeUpdatedRecord(Map<ResourceId, FieldValue> existingValues, FormInputModel inputModel) {
    // We inherit all the existing values...
    FormInstance record = new FormInstance(ResourceId.generateId(), formTree.getRootFormId());
    record.setAll(existingValues);
    // Now apply changes...
    for (FormTree.Node node : formTree.getRootFields()) {
        FieldInput fieldInput = inputModel.get(node.getFieldId());
        switch(fieldInput.getState()) {
            case UNTOUCHED:
                // No changes
                break;
            case EMPTY:
                record.set(node.getFieldId(), (FieldValue) null);
                break;
            case VALID:
                record.set(node.getFieldId(), fieldInput.getValue());
                break;
            case INVALID:
                LOGGER.info("Field with invalid input = " + node.getFieldId());
                record.set(node.getFieldId(), (FieldValue) null);
                break;
        }
    }
    LOGGER.info("fieldValues = " + record.getFieldValueMap());
    return record;
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) FormInstance(org.activityinfo.model.form.FormInstance)

Example 10 with FieldInput

use of org.activityinfo.ui.client.input.model.FieldInput in project activityinfo by bedatadriven.

the class GeoPointWidget method updateModel.

private void updateModel() {
    latitude.parseAndValidate();
    longitude.parseAndValidate();
    if (latitude.empty && longitude.empty) {
        // If both coordinates are empty, consider this to
        // be an empty field and clear the field validation messages
        // (the form may still mark this field as a whole if the field is
        // required, but we don't have to deal with that here)
        latitude.field.clearInvalid();
        longitude.field.clearInvalid();
        updater.update(FieldInput.EMPTY);
    } else {
        if (latitude.valid && longitude.valid) {
            updater.update(new FieldInput(new GeoPoint(latitude.value, longitude.value)));
        } else {
            latitude.markInvalidIfEmpty();
            longitude.markInvalidIfEmpty();
            updater.update(FieldInput.INVALID_INPUT);
        }
    }
}
Also used : GeoPoint(org.activityinfo.model.type.geo.GeoPoint) FieldInput(org.activityinfo.ui.client.input.model.FieldInput)

Aggregations

FieldInput (org.activityinfo.ui.client.input.model.FieldInput)12 RecordRef (org.activityinfo.model.type.RecordRef)6 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)5 Test (org.junit.Test)5 LocalDate (org.activityinfo.model.type.time.LocalDate)4 FormTree (org.activityinfo.model.formTree.FormTree)2 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)2 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)2 Quantity (org.activityinfo.model.type.number.Quantity)2 Date (java.util.Date)1 Set (java.util.Set)1 FormInstance (org.activityinfo.model.form.FormInstance)1 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 ReferenceValue (org.activityinfo.model.type.ReferenceValue)1 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)1 Month (org.activityinfo.model.type.time.Month)1 PeriodValue (org.activityinfo.model.type.time.PeriodValue)1