--- /dev/null
+package net.bananasplayground.validator;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+import org.apache.commons.lang3.StringUtils;
+
+import com.vaadin.data.ValidationResult;
+import com.vaadin.data.ValueContext;
+import com.vaadin.data.validator.AbstractValidator;
+
+/**
+ * @see net.bananasplayground.validator.TimeFrameValidatorDefinition
+ */
+
+public class TimeFrameValidator extends AbstractValidator<LocalDateTime> {
+
+ private final String errorMessage;
+ private final TimeFrameValidatorDefinition definition;
+
+ protected TimeFrameValidator(String errorMessage, TimeFrameValidatorDefinition definition) {
+ super(errorMessage);
+ this.errorMessage = errorMessage;
+ this.definition = definition;
+ }
+
+ @Override
+ public ValidationResult apply(LocalDateTime value, ValueContext context) {
+ if(StringUtils.isAnyBlank(definition.getTimeTo(), definition.getTimeFrom())) return ValidationResult.ok();
+ boolean isValid = false;
+
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+ LocalDateTime timeFrom = LocalDateTime.parse(value.format(formatter) +"T"+definition.getTimeFrom());
+ LocalDateTime timeTo = LocalDateTime.parse(value.format(formatter) +"T"+definition.getTimeTo());
+
+ if((value.isEqual(timeFrom) || value.isAfter(timeFrom)) && (value.isEqual(timeTo) || value.isBefore(timeTo))) {
+ isValid = true;
+ }
+
+ return isValid ? ValidationResult.ok() : ValidationResult.error(this.errorMessage);
+ }
+}
--- /dev/null
+package net.bananasplayground.validator;
+
+import info.magnolia.ui.field.ConfiguredFieldValidatorDefinition;
+import info.magnolia.ui.field.ValidatorType;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * Check if given datetime is between given timeframe of that date(day)
+ *
+ * works on LocalDateTime object
+ *
+ * timeframe:
+ * $type: timeframe
+ * timeFrom: "HH:MM"
+ * timeTo: "HH:MM"
+ * errorMessage: bla
+ */
+
+@Getter
+@Setter
+@ValidatorType("timeframe")
+public class TimeFrameValidatorDefinition extends ConfiguredFieldValidatorDefinition {
+
+ private String timeTo;
+ private String timeFrom;
+
+ public TimeFrameValidatorDefinition() {
+ setFactoryClass(TimeFrameValidatorFactory.class);
+ }
+}
--- /dev/null
+package net.bananasplayground.validator;
+
+import java.time.LocalDateTime;
+
+import javax.inject.Inject;
+
+import com.vaadin.data.Validator;
+
+import info.magnolia.ui.field.AbstractFieldValidatorFactory;
+
+/**
+ * @see net.bananasplayground.validator.TimeFrameValidatorDefinition
+ */
+
+public class TimeFrameValidatorFactory extends AbstractFieldValidatorFactory<TimeFrameValidatorDefinition, LocalDateTime> {
+
+ @Inject
+ public TimeFrameValidatorFactory(TimeFrameValidatorDefinition definition) {
+ super(definition);
+ }
+
+ @Override
+ public Validator<LocalDateTime> createValidator() {
+ return new TimeFrameValidator(getI18nErrorMessage(), definition);
+ }
+}