use of com.enonic.xp.form.FieldSet in project xp by enonic.
the class FormDefaultValuesProcessorImpl method processFormItems.
private void processFormItems(final Iterable<FormItem> formItems, final PropertySet dataSet) {
StreamSupport.stream(formItems.spliterator(), false).forEach(formItem -> {
if (formItem.getType() == INPUT) {
Input input = formItem.toInput();
if (input.getDefaultValue() != null) {
try {
final Value defaultValue = InputTypes.BUILTIN.resolve(input.getInputType()).createDefaultValue(input);
final PropertyPath propertyPath = PropertyPath.from(input.getName());
if (defaultValue != null && dataSet.getProperty(propertyPath) == null) {
if (input.getOccurrences().getMinimum() > 0) {
for (int i = 0; i < input.getOccurrences().getMinimum(); i++) {
dataSet.setProperty(input.getName(), i, defaultValue);
}
} else {
dataSet.setProperty(input.getName(), defaultValue);
}
}
} catch (IllegalArgumentException ex) {
LOG.warn("Invalid default value for " + input.getInputType() + " input type with name '" + input.getName() + "': '" + input.getDefaultValue().getRootValue() + "'" + (ex.getMessage() == null ? "" : " - " + ex.getMessage()));
}
}
} else if (formItem.getType() == FORM_ITEM_SET) {
processFormItems(formItem.getName(), formItem.toFormItemSet().getFormItems(), dataSet, formItem.toFormItemSet().getOccurrences().getMinimum());
} else if (formItem.getType() == LAYOUT && formItem.toLayout() instanceof FieldSet) {
processFormItems((FieldSet) formItem.toLayout(), dataSet);
} else if (formItem.getType() == FORM_OPTION_SET_OPTION) {
FormOptionSetOption option = formItem.toFormOptionSetOption();
if (option.isDefaultOption()) {
dataSet.setProperty("_selected", ValueFactory.newString(formItem.getName()));
if (dataSet.getProperty(formItem.getName()) == null) {
Property property = dataSet.setProperty(formItem.getName(), ValueFactory.newPropertySet(new PropertySet()));
processFormItems(option.getFormItems(), property.getSet());
}
}
} else if (formItem.getType() == FORM_OPTION_SET) {
processFormItems(formItem.getName(), formItem.toFormOptionSet().getFormItems(), dataSet, formItem.toFormOptionSet().getOccurrences().getMinimum());
}
});
}
use of com.enonic.xp.form.FieldSet in project xp by enonic.
the class MixinServiceImpl method transformFormItems.
private List<FormItem> transformFormItems(final Iterable<FormItem> iterable, final Set<MixinName> inlineMixinStack) {
final List<FormItem> formItems = new ArrayList<>();
for (final FormItem formItem : iterable) {
if (formItem instanceof InlineMixin) {
final InlineMixin inline = (InlineMixin) formItem;
final MixinName mixinName = inline.getMixinName();
final Mixin mixin = getByName(mixinName);
if (mixin != null) {
if (inlineMixinStack.contains(mixinName)) {
final String error = "Cycle detected in mixin [" + mixin.getName() + "]. It contains an inline mixin that references itself.";
LOG.error(error);
throw new IllegalArgumentException(error);
}
inlineMixinStack.add(mixinName);
final Form mixinForm = doInlineFormItems(mixin.getForm(), inlineMixinStack);
inlineMixinStack.remove(mixinName);
for (final FormItem mixinFormItem : mixinForm) {
formItems.add(mixinFormItem.copy());
}
} else {
throw new IllegalArgumentException("Inline mixin [" + mixinName + "] not found");
}
} else if (formItem instanceof FormItemSet) {
final FormItemSet.Builder formItemSetBuilder = FormItemSet.create((FormItemSet) formItem);
formItemSetBuilder.clearFormItems();
formItemSetBuilder.addFormItems(transformFormItems((FormItemSet) formItem, inlineMixinStack));
formItems.add(formItemSetBuilder.build());
} else if (formItem instanceof FieldSet) {
final FieldSet.Builder formItemSetBuilder = FieldSet.create((FieldSet) formItem);
formItemSetBuilder.clearFormItems();
formItemSetBuilder.addFormItems(transformFormItems((FieldSet) formItem, inlineMixinStack));
formItems.add(formItemSetBuilder.build());
} else if (formItem instanceof FormOptionSet) {
final FormOptionSet.Builder formOptionSetBuilder = FormOptionSet.create((FormOptionSet) formItem);
formOptionSetBuilder.clearOptions();
for (FormOptionSetOption option : (FormOptionSet) formItem) {
final FormOptionSetOption.Builder optionBuilder = FormOptionSetOption.create(option);
optionBuilder.clearFormItems();
optionBuilder.addFormItems(transformFormItems(option.getFormItems(), inlineMixinStack));
formOptionSetBuilder.addOptionSetOption(optionBuilder.build());
}
formItems.add(formOptionSetBuilder.build());
} else {
formItems.add(formItem.copy());
}
}
return formItems;
}
use of com.enonic.xp.form.FieldSet in project xp by enonic.
the class ContentTypeTest method layout_inside_formItemSet.
@Test
public void layout_inside_formItemSet() {
ContentType contentType = ContentType.create().name("myapplication:test").superType(ContentTypeName.structured()).build();
FieldSet layout = FieldSet.create().label("Personalia").name("personalia").addFormItem(Input.create().name("eyeColour").label("Eye color").inputType(InputTypeName.TEXT_LINE).build()).build();
FormItemSet myFormItemSet = FormItemSet.create().name("mySet").addFormItem(layout).build();
contentType.getForm().getFormItems().add(myFormItemSet);
assertEquals("mySet.eyeColour", contentType.getForm().getInput("mySet.eyeColour").getPath().toString());
}
use of com.enonic.xp.form.FieldSet in project xp by enonic.
the class ContentTypeHandlerTest method getForm.
private Form getForm() {
Input myTextLine = Input.create().name("myTextLine").inputType(InputTypeName.TEXT_LINE).label("My text line").customText("Some custom text").helpText("Some help text").required(true).inputTypeConfig(InputTypeConfig.create().property(InputTypeProperty.create("regexp", "\\b\\d{3}-\\d{2}-\\d{4}\\b").build()).build()).build();
Input myCustomInput = Input.create().name("myCheckbox").inputType(InputTypeName.CHECK_BOX).label("My checkbox input").required(false).defaultValue(InputTypeDefault.create().property(InputTypeProperty.create("default", "checked").build()).build()).build();
final InputTypeProperty option1 = InputTypeProperty.create("option", "Option One").attribute("value", "one").build();
final InputTypeProperty option2 = InputTypeProperty.create("option", "Option Two").attribute("value", "two").build();
Input radioButtonInput = Input.create().name("myRadioButton").inputType(InputTypeName.RADIO_BUTTON).label("Radio button").inputTypeConfig(InputTypeConfig.create().property(option1).property(option2).build()).build();
FieldSet myFieldSet = FieldSet.create().name("myFieldSet").label("My field set").addFormItem(Input.create().name("myTextLineInFieldSet").inputType(InputTypeName.TEXT_LINE).label("My text line").required(false).build()).build();
FormItemSet myFormItemSet = FormItemSet.create().name("myFormItemSet").label("My form item set").addFormItem(Input.create().name("myTextLine").inputType(InputTypeName.TEXT_LINE).label("My text line").required(false).build()).build();
final FormOptionSet formOptionSet = FormOptionSet.create().name("myOptionSet").label("My option set").helpText("Option set help text").addOptionSetOption(FormOptionSetOption.create().name("myOptionSetOption1").label("option label1").helpText("Option help text").addFormItem(Input.create().name("myTextLine1").label("myTextLine1").inputType(InputTypeName.TEXT_LINE).build()).build()).addOptionSetOption(FormOptionSetOption.create().name("myOptionSetOption2").label("option label2").helpText("Option help text").addFormItem(Input.create().name("myTextLine2").label("myTextLine2").inputType(InputTypeName.TEXT_LINE).build()).build()).build();
return Form.create().addFormItem(myTextLine).addFormItem(myCustomInput).addFormItem(radioButtonInput).addFormItem(myFieldSet).addFormItem(myFormItemSet).addFormItem(formOptionSet).build();
}
Aggregations