use of com.enonic.xp.form.FormItemSet in project xp by enonic.
the class OccurrenceValidatorTest method data_for_input_is_not_required_if_parent_data_set_does_not_exist.
@Test
public void data_for_input_is_not_required_if_parent_data_set_does_not_exist() {
Input myInput = Input.create().name("myRequiredInput").label("Input").inputType(InputTypeName.TEXT_LINE).required(true).build();
FormItemSet mySet = FormItemSet.create().name("mySet").required(false).addFormItem(myInput).build();
contentType.getForm().getFormItems().add(mySet);
Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
content.getData().setString("myData", "1");
// exercise
final ValidationErrors validationResults = validate(content);
assertFalse(validationResults.hasErrors());
}
use of com.enonic.xp.form.FormItemSet in project xp by enonic.
the class OccurrenceValidatorTest method given_required_input_at_top_and_inside_formItemSet_and_formItemSet_have_other_unrequired_data_when_validate_then_two_errors_are_found.
@Test
public void given_required_input_at_top_and_inside_formItemSet_and_formItemSet_have_other_unrequired_data_when_validate_then_two_errors_are_found() {
Input myInput = Input.create().name("myRequiredInput").label("Input").inputType(InputTypeName.TEXT_LINE).required(true).build();
FormItemSet mySet = FormItemSet.create().name("mySet").required(false).addFormItem(myInput).build();
contentType.getForm().getFormItems().add(mySet);
contentType.getForm().getFormItems().add(Input.create().name("myOtherRequiredInput").label("Other input").inputType(InputTypeName.TEXT_LINE).required(true).build());
Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
content.getData().setString("mySet.myUnrequiredData", "1");
assertEquals("mySet.myRequiredInput", mySet.getInput("myRequiredInput").getPath().toString());
// exercise
final ValidationErrors validationResults = validate(content);
assertThat(validationResults.stream()).hasSize(2).allMatch(ve -> ve instanceof DataValidationError).extracting(ValidationError::getArgs).containsExactly(Arrays.array(List.of("mySet.myRequiredInput", 1, 0), List.of("myOtherRequiredInput", 1, 0)));
}
use of com.enonic.xp.form.FormItemSet in project xp by enonic.
the class OccurrenceValidatorTest method given_formitemset_with_input_with_minOccur1_with_one_null_value_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError.
@Test
public void given_formitemset_with_input_with_minOccur1_with_one_null_value_when_validate_then_hasErrors_returns_MinimumOccurrencesValidationError() {
FormItemSet crimes = FormItemSet.create().name("crimes").multiple(true).build();
contentType.getForm().getFormItems().add(crimes);
crimes.add(Input.create().name("description").label("Description").minimumOccurrences(1).inputType(InputTypeName.TEXT_LINE).build());
crimes.add(Input.create().name("year").label("Year").inputType(InputTypeName.TEXT_LINE).build());
Content content = Content.create().path(MY_CONTENT_PATH).type(contentType.getName()).build();
content.getData().setString("crimes[0].description", null);
content.getData().setString("crimes[0].year", "1989");
// exercise
final ValidationErrors validationResults = validate(content);
assertTrue(validationResults.hasErrors());
assertThat(validationResults.stream()).allMatch(ve -> ve instanceof DataValidationError);
}
use of com.enonic.xp.form.FormItemSet 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.FormItemSet in project xp by enonic.
the class ModifyContentHandlerTest method mockXData.
private void mockXData() {
final FormItemSet cSet = FormItemSet.create().name("c").occurrences(0, 10).addFormItem(Input.create().label("d").name("d").inputType(InputTypeName.CHECK_BOX).build()).addFormItem(Input.create().label("e").name("e").occurrences(0, 0).inputType(InputTypeName.TEXT_LINE).build()).addFormItem(Input.create().label("f").name("f").inputType(InputTypeName.LONG).build()).build();
final ContentType contentType = ContentType.create().name("test:myContentType").superType(ContentTypeName.structured()).addFormItem(Input.create().label("a").name("a").inputType(InputTypeName.DOUBLE).build()).addFormItem(Input.create().label("b").name("b").inputType(InputTypeName.TEXT_LINE).build()).addFormItem(cSet).addFormItem(Input.create().label("z").name("z").occurrences(0, 10).inputType(InputTypeName.TEXT_LINE).build()).build();
GetContentTypeParams getContentType = GetContentTypeParams.from(ContentTypeName.from("test:myContentType"));
when(this.contentTypeService.getByName(eq(getContentType))).thenReturn(contentType);
final XData xData1 = XData.create().name(XDataName.from("com.enonic.myapplication:myschema")).addFormItem(Input.create().label("a").name("a").inputType(InputTypeName.DOUBLE).build()).build();
when(this.xDataService.getByName(eq(xData1.getName()))).thenReturn(xData1);
final XData xData2 = XData.create().name(XDataName.from("com.enonic.myapplication:other")).addFormItem(Input.create().label("name").name("name").inputType(InputTypeName.TEXT_LINE).build()).build();
when(this.xDataService.getByName(eq(xData1.getName()))).thenReturn(xData1);
when(this.xDataService.getByName(eq(xData2.getName()))).thenReturn(xData2);
when(this.mixinService.inlineFormItems(any(Form.class))).then(returnsFirstArg());
}
Aggregations