Spring MVC Data Binding Error - spring

I'm getting the following error when I try to retrieve the form results in controller method:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'search' on field 'clients': rejected value [14]; codes [typeMismatch.search.clients,typeMismatch.clients,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [search.clients,clients]; arguments []; default message [clients]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'clients'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.Client] for property 'clients[0]': no matching editors or conversion strategy found]
Model Object looks like this:
public class SearchForm {
private String name;
private List<Client> clients;
//getters and setters
}
public class Client {
private String name;
private Int id;
}
form:
<form:form method="POST", name="searchresults.html" modelattibute="search">
<form:input path="name"/>
<form:checkboxes path="clients" items="{clientsList}" itemsValue="id" itemsLabel="name"/>
</form:form>
this form displays the values correctly on the html page but when I click the submit button I get the above error

The browser will only send a list of client IDs when the form is submitted. How could Spring know how to convert each ID into a Client instance. You either have to tell it how to do, or add a List<String> clientIds property to your bean, and make the form:checkboxes tag use this property instead of clients as its path.

Related

Unable to submit list of LocalTime in Spring

I have this entity that contains a list of objects (omitted getters and setters and all irrelevant code for this example)
public class SampleType {
#OneToMany
List<SampleTypeTime> sampleTypeTimes = new ArrayList<SampleTypeTime>();
}
public class SampleTypeTime {
#DateTimeFormat(iso = ISO.TIME)
LocalTime time;
}
And i have this form that allows the user to select multiple hours..
<form th:object="${sampleType}" th:method="POST" th:action="#{#}">
<select th:field="*{sampleTypeTimes}" type="time" class="form-control" multiple>
<option th:value="00:00" th:text="${"00:00"}"></option>
<option th:value="01:00" th:text="${"01:00"}"></option>
... and so on
</select>
</form>
My controller:
#PostMapping("sampletype/")
public String productsTypesPost(#ModelAttribute SampleType sampleType, Model model) {
sampleTypeRepository.save(sampleType);
return "sampletype";
}
When i submit the form i get the following error message:
Field error in object 'sampleType' on field 'sampleTypeTimes': rejected value [00:00,02:00];
codes [typeMismatch.sampleType.sampleTypeTimes,typeMismatch.sampleTypeTimes,typeMismatch.java.util.List,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [sampleType.sampleTypeTimes,sampleTypeTimes];
arguments []; default message [sampleTypeTimes]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'sampleTypeTimes';
nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.example.project.SampleTypeTime' for property 'sampleTypeTimes[0]': no matching editors or conversion strategy found]
It seems to me that it struggles converting String[] to List, how can i get around this?
Edit: added controller class
As I said in my comment, form returns value of String while the array contains instances of SampleTypeTime. You need to tell Spring how to convert String to SampleTypeTime. For doing that you have to create implementation of PropertyEditor:
public class SampleTypeTimeEditor extends PropertyEditorSupport {
#Override
public void setAsText(String text) throws IllegalArgumentException {
LocalTime time = LocalTime.parse(text);
SampleTypeTime sampleTime = new SampleTypeTime();
sampleTime.setTime(time);
setValue(appointment);
}
}
In this example code snippet I don't check the text if it has a right format or not. But in real code you should do it of course. After that add the created property editor to the DataBinder of your controller:
#Controller
public class FormController {
#InitBinder
public void initBinder(DataBinder binder)
binder.registerCustomEditor(SampleTypeTime.class, new SampleTypeTimeEditor());
}
...
}
Now Spring automatically will convert String to SampleTypeTime. You can get more information about PropertyEditor from this chapter of the official documentation. And here you can get details about DataBinder.

Thymeleaf and SpringBoot - Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for prope

I am building a web application using Thymeleaf and SpringBoot, I am new to both the technologies.
In my html file, there is a Date field as follows:
<input type="date" th:field="*{issueDate}" />
My model class has a field corresponding to issueDate as follows:
#NotNull(message = "Cannot be empty")
private Date issueDate;
When I input a date from the UI, I see the following exception in the browser:
Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property issueDate;
From my experience, I understand that the UI reads the property as a String, but the model expects a type Date due to which the error occurs. So I need to convert the String to a Date. However where should this be done? Since the error occurs even before the setter method in the model is invoked.
Any help would be much appreciated! Thanks in advance!
In your controller:
#InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("MM/dd/yyyy"), true));
}
WHERE "MM/dd/yyyy" is the date format you're using.

Spring MVC Validation custom message

my problem is that I have a form which has html select element with some choosing option value & I want to validate those value using :
org.hibernate.validator.constraints
or
javax.validation.constraints
annotations.
here you may see my form & my select element:
<form:form action="../agents/add" method="POST" commandName="myAgent">
<form:select path="state">
<form:option value="ACTIVE" path="state">ACTIVE</form:option>
<form:option value="LISTEN" path="state">LISTEN</form:option>
<form:option value="DOWN" path="state">DOWN</form:option>
</form:select>
</form:form>
I have defined my controller method like this:
#RequestMapping(value = "agents/add", method = RequestMethod.POST)
public String addAgentSubmit(#ModelAttribute("myAgent") #Valid final AgentValidator agent, BindingResult result, RedirectAttributes redirect) {
if (result.hasErrors()) {
return "admin/agent/add";
}
...
}
and I also define a ModelAttribute like this:
#ModelAttribute("myAgent")
public AgentValidator getLoginForm() {
return new AgentValidator();
}
Here is my AgentValidator class also:
public class AgentValidator {
#NotEmpty(message = "your state can not be empty !")
private AgentState state;
getter & setter ...
}
and here is my AgentState:
public enum AgentState {
ACTIVE, DOWN, PAUSED
}
when I input a wrong value to my form (something like this):
<form:form action="../agents/add" method="POST" commandName="myAgent">
<form:select path="state">
<form:option value="ACTIVE!NVALID" path="state">ACTIVE</form:option>
<form:option value="LISTEN" path="state">LISTEN</form:option>
<form:option value="DOWN" path="state">DOWN</form:option>
</form:select>
</form:form>
after I submit my form I don't have a custom message showing in my JSP, instead I will see this message:
Failed to convert property value of type java.lang.String to required type tm.sys.validator.AgentState for property state; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type #javax.validation.constraints.NotNull tm.sys.validator.AgentState for value ACTIVE!NVALID; nested exception is java.lang.IllegalArgumentException: No enum constant tm.sys.validator.AgentState.ACTIVE!NVALID
I searched a lot for this problem but none of the solution helped me to provide my customized message showing to the user.
If you have any solution for this please provide it complete because I'm not so advance programmer yet.
You need to add one of the following keys: typeMismatch, typeMismatch.state or typeMismatch.agentValidator.state into your Messages.properties (or ValidationMessages.properties - whichever messages properties file you have configured with your validator).
typeMismatch.state=You provided invalid state
Exact key name(s) that you need add to message properties can be found from BindingResult.
Take a look at result.getFieldError().getCodes() (this should contain codes for the first field error if there is any).
This will override default message (the one which you are seeing) for situations when form submitted value cannot be converted to required type (enum in your case).

Spring-MVC bean Validation Type mismtach error

I am trying to validate an object using Spring JSR303 validation, i have a form object which have some nested objects along with some form properties here is my Form signature
public class PaymentDetailsForm
{
private AddressForm billingAddress;
// other properties and getter and setters
}
In my AddressForm bean i have used Bean validation annotations to validate data, but i am not using any #Valid annotation inside my PaymentDetailsForm for billingAddress.
This is the signature of my Controller method
public String createUpdatePaymentInfos(final Model model,
#ModelAttribute("paymentInfo") #Valid final PaymentDetailsForm form, final BindingResult bindingResult)
{
}
If i am sending correct data from the form everything is working perfectly fine, but if i omit any field from billingAddress which is marked as required or not null i am getting following binding error exception
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'paymentInfo' on field 'billingAddress':
rejected value [com.xxx.storefront.forms.AddressForm#e39f6f1,true];
codes [typeMismatch.paymentInfo.billingAddress,typeMismatch.billingAddress,typeMismatch.com.xxx.storefront.forms.AddressForm,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable:
codes [paymentInfo.billingAddress,billingAddress]; arguments []; default message [billingAddress]];
default message [Failed to convert property value of type 'java.lang.String[]'
to required type 'com.xxx.storefront.forms.AddressForm' for property 'billingAddress';
nested exception is java.lang.IllegalStateException:
Cannot convert value of type [java.lang.String[]] to required type [com.xxx.storefront.forms.AddressForm] for property 'billingAddress':
no matching editors or conversion strategy found]
I was expecting that since i have not used #valid annotation for billingAddress property, it should not be validated but even in case it get validated i am not able to understand above mentioned exception/error
The bindingResult that you are seeing doesn't look like it is because of validation errors, it is likely because of the binding errors - not being able to bind the UI fields to the inner billingAddress field. Even the binding errors would end up showing in the immediately following bindingresult argument like you are seeing.
That was due to some wrong mapping from UI,in my JSP page i was mapping address fields to billingAddress object but there was one hidden field like
<form:hidden path="billingAddress" id="billingAddress"/>
This was the cause of error since it was trying to send String array and Spring binding was unable to distinguish what i am trying to do

Wrapping PropertyEdtior exceptions during Spring validation

When I submit a Spring form and a PropertyEditor fails to convert a value, an exception is thrown and a message like this ends up in my validator errors object:
Failed to convert property value of type java.lang.String to required type
org.joda.time.DateMidnight for property startDate; nested exception is
java.lang.IllegalArgumentException: Invalid format: "2010-111" is malformed at "1"
Can I wrap this somehow, providing a friendlier message to display on screen?
I'm using #Valid in my controller, with the following in my form:
#NotNull
protected DateMidnight startDate;
Thanks
This error message has message codes such as typeMismatch.objectName.startDate (see DefaultMessageCodesResolver). To display a user-friendly message, you need to declare a MessageSource with the corresponding messages.

Resources