springframework.validation.BindException: Date

@Controller
public class WelcomeController {
@GetMapping("/welcome")
public String mainWithParam(
@RequestParam(name = "name", required = false, defaultValue = "") String name, Model model,
@ModelAttribute ReservationForm form) {

model.addAttribute("form", form);
return "welcome";
}

@PostMapping("/welcome/a")
public String datePickerOnChange(@ModelAttribute("form") ReservationForm form, Model model,
@RequestParam String date) {
System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
System.out.println(date);
System.out.println(form.getDate());
//set date on form to new date
//update data

/* [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'form' on field 'date': rejected value [2023-11-29T11:20:17.719199]; codes [typeMismatch.form.date,typeMismatch.date,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [form.date,date]; arguments []; default message [date]]; default message [Failed to convert value of type 'java.lang.String[]' to required type 'java.time.LocalDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '2023-11-29T11:20:17.719199'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-11-29T11:20:17.719199]]]
*/
return "welcome";
}
}
@Controller
public class WelcomeController {
@GetMapping("/welcome")
public String mainWithParam(
@RequestParam(name = "name", required = false, defaultValue = "") String name, Model model,
@ModelAttribute ReservationForm form) {

model.addAttribute("form", form);
return "welcome";
}

@PostMapping("/welcome/a")
public String datePickerOnChange(@ModelAttribute("form") ReservationForm form, Model model,
@RequestParam String date) {
System.out.println("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
System.out.println(date);
System.out.println(form.getDate());
//set date on form to new date
//update data

/* [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'form' on field 'date': rejected value [2023-11-29T11:20:17.719199]; codes [typeMismatch.form.date,typeMismatch.date,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [form.date,date]; arguments []; default message [date]]; default message [Failed to convert value of type 'java.lang.String[]' to required type 'java.time.LocalDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '2023-11-29T11:20:17.719199'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-11-29T11:20:17.719199]]]
*/
return "welcome";
}
}
12 Replies
JavaBot
JavaBot12mo ago
This post has been reserved for your question.
Hey @' .• KIΛ.RΛR ’•. '! Please use /close or the Close Post` button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
' .•` KIΛ.RΛR ’•. '
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<title>Spring Boot Thymeleaf Hello World Example</title>
<link rel="stylesheet" th:href="@{webjars/bootstrap/4.2.1/css/bootstrap.min.css}" />
<link rel="stylesheet" th:href="@{/css/main.css}" />

</head>
<body>
<form th:object="${form}">
<div>
<label for="reservation-date"></label>
<input class="formContent" type="datetime-local" th:field="*{date}"
th:onchange="reloadAlleyOptions(this.getAttribute('data-date'))" th:data-date="*{date}" />
</div>

<button type="submit"></button>

</form>
</body>

<script src="https://code.jquery.com/jquery-3.7.1.min.js"
integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>

<script>
$(document).bind("ajaxSend", function () {
console.log("waiting for all requests to complete...");
}).bind("ajaxStop", function () {
location.reload();
});

function reloadAlleyOptions(date) {
console.log(date)
console.log("---")
$.ajax({
method: "post",
type: "POST",
url: "/welcome/a?date=" + date,
success: function (response) {

},
error: function (e) {}
});
}
</script>

</html>
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<title>Spring Boot Thymeleaf Hello World Example</title>
<link rel="stylesheet" th:href="@{webjars/bootstrap/4.2.1/css/bootstrap.min.css}" />
<link rel="stylesheet" th:href="@{/css/main.css}" />

</head>
<body>
<form th:object="${form}">
<div>
<label for="reservation-date"></label>
<input class="formContent" type="datetime-local" th:field="*{date}"
th:onchange="reloadAlleyOptions(this.getAttribute('data-date'))" th:data-date="*{date}" />
</div>

<button type="submit"></button>

</form>
</body>

<script src="https://code.jquery.com/jquery-3.7.1.min.js"
integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>

<script>
$(document).bind("ajaxSend", function () {
console.log("waiting for all requests to complete...");
}).bind("ajaxStop", function () {
location.reload();
});

function reloadAlleyOptions(date) {
console.log(date)
console.log("---")
$.ajax({
method: "post",
type: "POST",
url: "/welcome/a?date=" + date,
success: function (response) {

},
error: function (e) {}
});
}
</script>

</html>
' .•` KIΛ.RΛR ’•. '
public class ReservationForm {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime date;

public ReservationForm(LocalDateTime date) {
this.date = LocalDateTime.now();
}

public LocalDateTime getDate() {
return date;
}

public void setDate(LocalDateTime date) {
this.date = date;
}

}
public class ReservationForm {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
private LocalDateTime date;

public ReservationForm(LocalDateTime date) {
this.date = LocalDateTime.now();
}

public LocalDateTime getDate() {
return date;
}

public void setDate(LocalDateTime date) {
this.date = date;
}

}
what i want: datepicker sends ajax request on changing the date to reload the displayed data, but when i change the date an error is thrown. demo project to showcase my problem: https://github.com/DragonCat4012/web-thymeleaf
GitHub
GitHub - DragonCat4012/web-thymeleaf
Contribute to DragonCat4012/web-thymeleaf development by creating an account on GitHub.
' .•` KIΛ.RΛR ’•. '
like in the javascript function it always prints the current date, even when i change the date in the picker it wont change anything =.=
Pleezon
Pleezon12mo ago
Have you tried parsing your string using
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = formatter.parse("2023-11-29T11:20:17.719199");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = formatter.parse("2023-11-29T11:20:17.719199");
I have a feeling this won't work because the string looks sketchy in regards to the format pattern (I sadly can't try it rn)
' .•` KIΛ.RΛR ’•. '
an welcher stelle O.o problem ist spring sagt das sobald ich das sobald ich nen datum gesetzt hab:
Field error in object 'reservationForm' on field 'date': rejected value [13/11/2023 12:32]; codes [typeMismatch.reservationForm.date,typeMismatch.date,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [reservationForm.date,date]; arguments []; default message [date]]; default message [Failed to convert value of type 'java.lang.String[]' to required type 'java.time.LocalDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '13/11/2023 12:32'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [13/11/2023 12:32]]]
Field error in object 'reservationForm' on field 'date': rejected value [13/11/2023 12:32]; codes [typeMismatch.reservationForm.date,typeMismatch.date,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [reservationForm.date,date]; arguments []; default message [date]]; default message [Failed to convert value of type 'java.lang.String[]' to required type 'java.time.LocalDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '13/11/2023 12:32'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [13/11/2023 12:32]]]
ich komm im java nciht mla zu irgendeinem print in der mapping oder der setter methode ;-; in der url hab ich als wert das ausgewählte datum als paramter wies sein soll ;-; nur ist die seite halt put dann
http://localhost:8080/welcome?date=13%2F11%2F2023+12%3A34
http://localhost:8080/welcome?date=13%2F11%2F2023+12%3A34
' .•` KIΛ.RΛR ’•. '
dafuq the datepicker shows a different date then its value?
Pleezon
Pleezon12mo ago
Your ReservationForm specifies a datetime pattern in @DateTimeFormat decorator I doubt your pattern would work considering "2023-11-29T11:20:17.719199" as possible input
' .•` KIΛ.RΛR ’•. '
yes? yeah ik but i dont know how to change it since thymeleaf does its shit wjho knows where in the background
Pleezon
Pleezon12mo ago
the correct format would be something along the lines of yyyy-MM-dd'T'HH:mm:ss.SSSSSS
public class ReservationForm {
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
private LocalDateTime date;

public ReservationForm(LocalDateTime date) {
this.date = LocalDateTime.now();
}

public LocalDateTime getDate() {
return date;
}

public void setDate(LocalDateTime date) {
this.date = date;
}

}
public class ReservationForm {
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS")
private LocalDateTime date;

public ReservationForm(LocalDateTime date) {
this.date = LocalDateTime.now();
}

public LocalDateTime getDate() {
return date;
}

public void setDate(LocalDateTime date) {
this.date = date;
}

}
@' .•` KIΛ.RΛR ’•. ' that should at least make it parse correctly
JavaBot
JavaBot12mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server