Search in sources :

Example 21 with FormItemSet

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());
}
Also used : Input(com.enonic.xp.form.Input) ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) FormItemSet(com.enonic.xp.form.FormItemSet) Test(org.junit.jupiter.api.Test)

Example 22 with FormItemSet

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)));
}
Also used : Input(com.enonic.xp.form.Input) ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) FormItemSet(com.enonic.xp.form.FormItemSet) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 23 with FormItemSet

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);
}
Also used : ValidationErrors(com.enonic.xp.content.ValidationErrors) Content(com.enonic.xp.content.Content) FormItemSet(com.enonic.xp.form.FormItemSet) DataValidationError(com.enonic.xp.content.DataValidationError) Test(org.junit.jupiter.api.Test)

Example 24 with FormItemSet

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;
}
Also used : FormOptionSet(com.enonic.xp.form.FormOptionSet) Form(com.enonic.xp.form.Form) FormItem(com.enonic.xp.form.FormItem) ArrayList(java.util.ArrayList) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) Mixin(com.enonic.xp.schema.mixin.Mixin) InlineMixin(com.enonic.xp.form.InlineMixin) MixinName(com.enonic.xp.schema.mixin.MixinName) FieldSet(com.enonic.xp.form.FieldSet) InlineMixin(com.enonic.xp.form.InlineMixin) FormItemSet(com.enonic.xp.form.FormItemSet)

Example 25 with FormItemSet

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());
}
Also used : GetContentTypeParams(com.enonic.xp.schema.content.GetContentTypeParams) ContentType(com.enonic.xp.schema.content.ContentType) Form(com.enonic.xp.form.Form) XData(com.enonic.xp.schema.xdata.XData) 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