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