How to avoid rejected value [] in spring submit form

I have two dates in a submit form in Spring 3 + Hibernate. @Column (name = "FinStartDate") private Date finStartDate;

@Column(name = "FinEndDate")
private Date finEndDate;

      

I am showing / hiding dates based on some criteria. When the dates are hidden and submit the form following errors

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'register' on field 'obj.finEndDate': rejected value []; codes [typeMismatch]

      

How to avoid the problem.

+3


source to share


2 answers


I think you are going to miss the formatter to convert a String date to a Date object.

You can try to annotate your field

@DateTimeFormat(pattern = "yyyy-MM-dd")

      



or declare initbinder in your controller like:

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(
            dateFormat, false));
}

      

Or, you can declare a formatter in your mvc config file that will format each Date object your application binds to.

0


source


@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@DateTimeFormat(pattern = "dd.MM.yyyy")
private Date finEndDate;

      



Perhaps you should use a serializer / deserializer.

0


source







All Articles