Search in sources :

Example 56 with Input

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

the class TimeTypeTest method testCreateDefaultValue_invalid.

@Test
public void testCreateDefaultValue_invalid() {
    final Input input = getDefaultInputBuilder(InputTypeName.TIME, "25:08:08").build();
    assertThrows(IllegalArgumentException.class, () -> this.type.createDefaultValue(input));
}
Also used : Input(com.enonic.xp.form.Input) Test(org.junit.jupiter.api.Test)

Example 57 with Input

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

the class InputValidationVisitorTest method validateInputTypeValid.

@Test
public void validateInputTypeValid() throws Exception {
    Input myTextLine = Input.create().name("myTextLine").inputType(InputTypeName.TEXT_LINE).label("My text line").required(true).build();
    Form form = Form.create().addFormItem(myTextLine).build();
    PropertyTree propertyTree = new PropertyTree();
    propertyTree.setString("myTextLine", "33");
    final InputValidationVisitor validationVisitor = new InputValidationVisitor(propertyTree, InputTypes.BUILTIN);
    validationVisitor.traverse(form);
}
Also used : Input(com.enonic.xp.form.Input) Form(com.enonic.xp.form.Form) PropertyTree(com.enonic.xp.data.PropertyTree) Test(org.junit.jupiter.api.Test)

Example 58 with Input

use of com.enonic.xp.form.Input 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 59 with Input

use of com.enonic.xp.form.Input 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 60 with Input

use of com.enonic.xp.form.Input 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());
        }
    });
}
Also used : Input(com.enonic.xp.form.Input) FieldSet(com.enonic.xp.form.FieldSet) Value(com.enonic.xp.data.Value) PropertyPath(com.enonic.xp.data.PropertyPath) PropertySet(com.enonic.xp.data.PropertySet) FormOptionSetOption(com.enonic.xp.form.FormOptionSetOption) Property(com.enonic.xp.data.Property)

Aggregations

Input (com.enonic.xp.form.Input)62 Test (org.junit.jupiter.api.Test)47 Value (com.enonic.xp.data.Value)24 Form (com.enonic.xp.form.Form)15 PropertyTree (com.enonic.xp.data.PropertyTree)11 FormItem (com.enonic.xp.form.FormItem)9 FormItemSet (com.enonic.xp.form.FormItemSet)9 FormDefaultValuesProcessor (com.enonic.xp.form.FormDefaultValuesProcessor)7 ContentType (com.enonic.xp.schema.content.ContentType)7 FormOptionSet (com.enonic.xp.form.FormOptionSet)5 FieldSet (com.enonic.xp.form.FieldSet)4 FormOptionSetOption (com.enonic.xp.form.FormOptionSetOption)4 PatternIndexConfigDocument (com.enonic.xp.index.PatternIndexConfigDocument)4 Property (com.enonic.xp.data.Property)3 Occurrences (com.enonic.xp.form.Occurrences)3 Content (com.enonic.xp.content.Content)2 ExtraData (com.enonic.xp.content.ExtraData)2 ValidationErrors (com.enonic.xp.content.ValidationErrors)2 PropertyPath (com.enonic.xp.data.PropertyPath)2 PropertySet (com.enonic.xp.data.PropertySet)2