Search in sources :

Example 6 with FormItemSet

use of com.enonic.xp.form.FormItemSet in project xp by enonic.

the class FormDefaultValuesProcessorImplTest method testDefaultValueForInputAndItemSetWithOccurrences.

@Test
public void testDefaultValueForInputAndItemSetWithOccurrences() {
    Input input = Input.create().name("testInput").label("testInput").inputType(InputTypeName.TEXT_LINE).defaultValue(InputTypeDefault.create().property(InputTypeProperty.create("default", "Default Value").build()).build()).build();
    FormItemSet formItemSet = FormItemSet.create().name("field").label("field").addFormItem(input).occurrences(Occurrences.create(3, 3)).build();
    final Form form = Form.create().addFormItem(formItemSet).build();
    final FormDefaultValuesProcessor defaultValuesProcessor = new FormDefaultValuesProcessorImpl();
    final PropertyTree data = new PropertyTree();
    defaultValuesProcessor.setDefaultValues(form, data);
    for (int i = 0; i < 3; i++) {
        assertEquals("Default Value", data.getProperty("field", i).getSet().getString("testInput"));
    }
}
Also used : Input(com.enonic.xp.form.Input) Form(com.enonic.xp.form.Form) FormDefaultValuesProcessor(com.enonic.xp.form.FormDefaultValuesProcessor) PropertyTree(com.enonic.xp.data.PropertyTree) FormItemSet(com.enonic.xp.form.FormItemSet) Test(org.junit.jupiter.api.Test)

Example 7 with FormItemSet

use of com.enonic.xp.form.FormItemSet in project xp by enonic.

the class FormDefaultValuesProcessorImplTest method testItemSetWithMinOccurrencesMoreZero.

@Test
public void testItemSetWithMinOccurrencesMoreZero() {
    FormOptionSet.Builder checkOptionSet = FormOptionSet.create().required(false).name("checkOptionSet").occurrences(Occurrences.create(1, 1));
    FormItemSet.Builder formItemSet = FormItemSet.create().name("item-set").occurrences(Occurrences.create(1, 1)).addFormItem(Input.create().name("testInput").label("testInput").inputType(InputTypeName.TEXT_LINE).build());
    FormOptionSetOption.Builder option = FormOptionSetOption.create().name("option_1").defaultOption(true).addFormItem(formItemSet.build());
    checkOptionSet.addOptionSetOption(option.build());
    final Form form = Form.create().addFormItem(checkOptionSet.build()).build();
    final FormDefaultValuesProcessor defaultValuesProcessor = new FormDefaultValuesProcessorImpl();
    final PropertyTree data = new PropertyTree();
    defaultValuesProcessor.setDefaultValues(form, data);
    assertEquals(4, data.getTotalSize());
    assertNotNull(data.getProperty("checkOptionSet"));
    assertEquals("option_1", data.getString("checkOptionSet._selected"));
    assertNotNull(data.getProperty("checkOptionSet.option_1.item-set"));
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) Form(com.enonic.xp.form.Form) FormDefaultValuesProcessor(com.enonic.xp.form.FormDefaultValuesProcessor) PropertyTree(com.enonic.xp.data.PropertyTree) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) FormItemSet(com.enonic.xp.form.FormItemSet) Test(org.junit.jupiter.api.Test)

Example 8 with FormItemSet

use of com.enonic.xp.form.FormItemSet in project xp by enonic.

the class FormJsonToPropertyTreeTranslatorTest method createFormForFieldSet.

private Form createFormForFieldSet() {
    final FieldSet fieldSet1 = FieldSet.create().name("properties").label("Properties").addFormItem(Input.create().name("number").label("Number").inputType(InputTypeName.LONG).build()).build();
    final FormItemSet itemSet = FormItemSet.create().name("attributes").occurrences(0, 0).addFormItem(Input.create().name("key").label("Key").inputType(InputTypeName.TEXT_LINE).build()).addFormItem(fieldSet1).build();
    final FieldSet fieldSet = FieldSet.create().name("attributes").label("Attributes").addFormItem(itemSet).build();
    return Form.create().addFormItem(fieldSet).build();
}
Also used : FieldSet(com.enonic.xp.form.FieldSet) FormItemSet(com.enonic.xp.form.FormItemSet)

Example 9 with FormItemSet

use of com.enonic.xp.form.FormItemSet in project xp by enonic.

the class OccurrenceValidator method validateOccurrences.

private static void validateOccurrences(final FormItem formItem, final List<PropertySet> parentDataSets, final ValidationErrors.Builder validationErrorsBuilder) {
    final Occurrences occurrences;
    if (formItem instanceof Input) {
        occurrences = ((Input) formItem).getOccurrences();
    } else if (formItem instanceof FormItemSet) {
        occurrences = ((FormItemSet) formItem).getOccurrences();
    } else if (formItem instanceof FormOptionSet) {
        occurrences = ((FormOptionSet) formItem).getOccurrences();
    } else {
        return;
    }
    for (final PropertySet parentDataSet : parentDataSets) {
        final int entryCount = parentDataSet.countProperties(formItem.getName());
        final PropertyPath parentPath = Optional.ofNullable(parentDataSet.getProperty()).map(Property::getPath).orElseGet(() -> PropertyPath.from(formItem.getPath().getParent().toString()));
        final PropertyPath path = PropertyPath.from(parentPath, formItem.getName());
        final int minOccurrences = occurrences.getMinimum();
        if (occurrences.impliesRequired() && entryCount < minOccurrences) {
            validationErrorsBuilder.add(ValidationError.dataError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "cms.occurrencesInvalid"), path).i18n("system.cms.validation.minOccurrencesInvalid").args(formItem.getPath(), minOccurrences, entryCount).build());
        }
        final int maxOccurrences = occurrences.getMaximum();
        if (maxOccurrences > 0 && entryCount > maxOccurrences) {
            validationErrorsBuilder.add(ValidationError.dataError(ValidationErrorCode.from(ApplicationKey.SYSTEM, "cms.occurrencesInvalid"), path).i18n("system.cms.validation.maxOccurrencesInvalid").args(formItem.getPath(), occurrences.getMaximum(), entryCount).build());
        }
    }
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) Input(com.enonic.xp.form.Input) PropertyPath(com.enonic.xp.data.PropertyPath) Occurrences(com.enonic.xp.form.Occurrences) PropertySet(com.enonic.xp.data.PropertySet) FormItemSet(com.enonic.xp.form.FormItemSet)

Example 10 with FormItemSet

use of com.enonic.xp.form.FormItemSet in project xp by enonic.

the class OccurrenceValidator method validate.

private static void validate(final Iterable<FormItem> formItems, final List<PropertySet> parentDataSets, final ValidationErrors.Builder validationErrorsBuilder) {
    for (FormItem formItem : formItems) {
        validateOccurrences(formItem, parentDataSets, validationErrorsBuilder);
        if (formItem instanceof FormItemSet) {
            final List<PropertySet> dataSets = getDataSets(formItem.getName(), parentDataSets);
            validate(((FormItemSet) formItem).getFormItems(), dataSets, validationErrorsBuilder);
        } else if (formItem instanceof FieldSet) {
            validate(((FieldSet) formItem).formItemIterable(), parentDataSets, validationErrorsBuilder);
        } else if (formItem instanceof FormOptionSet) {
            validateFormOptionSetSelection((FormOptionSet) formItem, parentDataSets, validationErrorsBuilder);
        }
    }
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) FieldSet(com.enonic.xp.form.FieldSet) FormItem(com.enonic.xp.form.FormItem) PropertySet(com.enonic.xp.data.PropertySet) FormItemSet(com.enonic.xp.form.FormItemSet)

Aggregations

FormItemSet (com.enonic.xp.form.FormItemSet)27 Test (org.junit.jupiter.api.Test)17 FormOptionSet (com.enonic.xp.form.FormOptionSet)10 Form (com.enonic.xp.form.Form)9 Input (com.enonic.xp.form.Input)8 PropertyTree (com.enonic.xp.data.PropertyTree)6 FieldSet (com.enonic.xp.form.FieldSet)6 Content (com.enonic.xp.content.Content)5 ValidationErrors (com.enonic.xp.content.ValidationErrors)5 FormItem (com.enonic.xp.form.FormItem)5 FormOptionSetOption (com.enonic.xp.form.FormOptionSetOption)5 ContentType (com.enonic.xp.schema.content.ContentType)5 DataValidationError (com.enonic.xp.content.DataValidationError)3 FormDefaultValuesProcessor (com.enonic.xp.form.FormDefaultValuesProcessor)3 PropertySet (com.enonic.xp.data.PropertySet)2 InlineMixin (com.enonic.xp.form.InlineMixin)2 Occurrences (com.enonic.xp.form.Occurrences)2 GetContentTypeParams (com.enonic.xp.schema.content.GetContentTypeParams)2 XData (com.enonic.xp.schema.xdata.XData)2 CreateContentParams (com.enonic.xp.content.CreateContentParams)1