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"));
}
}
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"));
}
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();
}
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());
}
}
}
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);
}
}
}
Aggregations