Search code examples
javaspring-bootthymeleaflocaldate

How do I validate between two dates using LocalTime using Springboot and Thymeleaf


I have a class with this field in:

@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate passDate;

I created two LocalDate variables which I want to check between (for example)

LocalDate a = LocalDate.of(1995, 1, 01); LocalDate b = LocalDate.of(2140, 12, 31);

I was using @PastOrPresent but that does not stop users from entering a date like year 3050.

I started making a method in the domain class, where the passDate field resides, however I really do not know where the validation goes and how to call this validation method. (Here is a snippet of what I was trying to do but unsure where to put it? (maybe its wrong too!))

if (!(passDate.isBefore(a) && passDate.isAfter(b))) {
return passDate; }

Wasn't sure where this goes? What method? How do I call this validation? or is there another way. I have looked online for so long and can't figure out what to do. I have a thymeleaf form with this field (which was using the PastOrPresent validation to return an error message on submit)

            <div class="form-group">
                <label for="pass_date">Enter the pass date</label>
                <input type="date" th:field="*{passDate}" name="pass_date" id="pass_date"
                       class="form-control"/>
                <p class="text-danger" th:if="${#fields.hasErrors('passDate')}" th:errors="*{passDate}"></p>
            </div>

Here is the post controller

@PostMapping("/admin/examform")
public String createExamForm(@ModelAttribute("examform") @Valid Examform examform,
                                    BindingResult bindingResult,
                                    @AuthenticationPrincipal final User user, Model model){
    if (bindingResult.hasErrors()) {
        System.out.println(bindingResult.getAllErrors());
        model.addAttribute("examform", examform);
        return "examformhtml";
    }else{
        examformservice.createExamForm(examform);
        model.addAttribute("loggedInUsername", user.getUsername());
        return "examformsuccess";
    }

}

Where examformservice is a class variable of my service which links to my repository which is

@Override
public void createExamForm(Examform examform) {
    String sql = "UPDATE examform SET passDate=? WHERE studentId=?";
    jdbcTemplate.update(sql, examform.getPassDate(), examform.getStudentId());

}

Where would I put the validation? and what would the input be?


Solution

  • If you want a JSR annotation you can work your way from this one:

    @Constraint(validatedBy=AfterValidator.class)
    @Target({ElementType.FIELD, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface After {
        String message() default "must be after {value}";
        Class<?>[] groups() default {};
        Class<? extends Payload>[] payload() default {};
        String value();
    }
    

    And its validator:

    public class AfterValidator implements ConstraintValidator<After, LocalDate> {
    
        private LocalDate date;
    
        public void initialize(After annotation) {
            date = LocalDate.parse(annotation.value());
        }
    
        public boolean isValid(LocalDate value, ConstraintValidatorContext context) {
            boolean valid = true;
            if (value != null) {
                if (!value.isAfter(date)) {
                    valid = false;
                }
            }
            return valid;
        }
    }
    

    The above is exclusive (the exact date will be invalid), you may want to tweak it.

    To use it is only to add annotation in model bean:

    @After("1995-01-01")
    private LocalDate passDate;
    

    The inverse (@Before) I leave you as an exercise :)